Message Boards Message Boards

0
|
7691 Views
|
4 Replies
|
5 Total Likes
View groups...
Share
Share this post:

can we Map other than First and Last, but another specific sub-element?

Posted 11 years ago
If we look at this code, I can Map First and Last, but I can't seem to find a way to Map to the middle term, or any other term. I have tried Extract and Select in differing guises but can't seem to find a way to do it. 
 b = Subsets[Range[2, 10], {3}];
 c = First /@ b;
 d = Last /@ b;
 
 Print[b[[1 ;; 10]],"  ",c[[1 ;; 10]],"   ",d[[1 ;; 10]]];
 
 {2, 3, 4}, {2, 3, 5}, {2, 3, 6}, {2, 3, 7}, {2, 3, 8},
 {2, 3, 9}, {2, 3, 10}, {2, 4, 5}, {2, 4, 6}, {2, 4, 7}
 {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
{4, 5, 6, 7, 8, 9, 10, 5, 6, 7}

I would like to extract the middle terms too and extend it to extract any term.

Paul.
POSTED BY: Paul Cleary
4 Replies
Posted 11 years ago
Thank you everyone for your quick replies, I had indeed used the method you sugged Chip on many occasion, It seems I had a momentary lapse of reason.  I was trying to mix the b[[All, 2]] in with the select and mapping method which obviously didnt work, there is also another less efficient way of doing it too, RotateRight also maps across lists so I could have used First /@ b then RotateRight /@ b and repeated the process.  Shenghui, I will take a look at those references for a refresher thank you.

Paul.
POSTED BY: Paul Cleary
Once you get familiar with the following functions, you are the guru of list manipulation: 

Specify range: 
1. Span: http://reference.wolfram.com/mathematica/ref/Span.html
2. Take: http://reference.wolfram.com/mathematica/ref/Take.html

Specify a condition: 
1.Cases:  http://reference.wolfram.com/mathematica/ref/Cases.html
2. Select: http://reference.wolfram.com/mathematica/ref/Select.html
3. Pick: http://reference.wolfram.com/mathematica/ref/Pick.html
POSTED BY: Shenghui Yang
Posted 11 years ago
You can also use All instead of Map.
 b = Subsets[Range[2, 10], {3}];
 
 In[1]:= b[[All, 1]]
 Out[1]= {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
 2, 2, 2, 2, 2, 2, 2,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, 6, 7, 7, 7, 8}
 
 In[6]:= t[[2]]
Out[6]= {3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6,
6, 6, 7, 7, 7, 8, 8, 9, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6,
7, 7, 7, 8, 8, 9, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 6, 6,
6, 6, 7, 7, 7, 8, 8, 9, 7, 7, 7, 8, 8, 9, 8, 8, 9, 9}

In[7]:= t[[3]]
Out[7]= {4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 7,
8, 9, 10, 8, 9, 10, 9, 10, 10, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 7,
8, 9, 10, 8, 9, 10, 9, 10, 10, 6, 7, 8, 9, 10, 7, 8, 9, 10, 8, 9, 10,
9, 10, 10, 7, 8, 9, 10, 8, 9, 10, 9, 10, 10, 8, 9, 10, 9, 10, 10, 9,
10, 10, 10}
POSTED BY: Greg Hurst
First and Last are special cases of Part, so one could map a function like Part[#, k]&
POSTED BY: Ilian Gachevski
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