Message Boards Message Boards

1
|
3412 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How do I apply the "Take" function to lists?

Posted 11 years ago

The following code produces three lists. List px contains 20 integers, list bx contains 10 integers and list cx contains 10 integers. I would like to apply the "Take" function to these 3 lists. I am trying to avoid having 100 separate "Take" statements in another worksheet. I keep getting the error below. I would like the output to be in a flatten-able list or a single list. What am I doing wrong!

    (*p=Table[p,{p,1,40,2}]
    Length[p];

    b=Table[b,{b,1,10,1}]
    Length[b];

    c=Table[c,{c,4,13,1}]
    Length[c];*)

    p = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 
       35, 37, 39};

    b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    c = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

    Take[p, {b, c}]

    Take::seqs: Sequence specification (+n, -n, {+n}, {-n}, {m, n}, or {m, n, s}) expected at position 2 in Take[{1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39},
{{1,2,3,4,5,6,7,8,9,10},{4,5,6,7,8,9,10,11,12,13}}]. >>
    Take[{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 
      35, 37, 39}, {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {4, 5, 6, 7, 8, 9, 
       10, 11, 12, 13}}]
POSTED BY: Kent J Jarvis
4 Replies
Posted 11 years ago

OK!

Thanks again Marco. It works fine.

Best wishes, Kent

POSTED BY: Kent J Jarvis

Sorry,

copied something wrong. One flatten was missing - then the join is not needed anymore either:

Flatten[ Take[p, {#}] & /@ Flatten[{b, c}]]

Cheers, M.

POSTED BY: Marco Thiel
Posted 11 years ago

Thanks Marco,

I am getting the same error. I don't know what is going on. Is there another method I could try? I will edit my first post. I changed the list names to avoid any conflicts (if any).

POSTED BY: Kent J Jarvis

Hi,

what about:

Flatten[Take[p, {#}] & /@ Join[{ b, c}]]

Cheers, M.

PS: There seems to be little confusion in your post about p-> px, b-> bx, and c-> cx.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract