Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list such as list = {1,4,3,4,6,3,1,2,3,4 ... } and a range, say, range = Range[20000].

I want to obtain the following output:

{{0,1}, {1,4}, {2,3}, {3,4}, {4,6}, ... {nth element of list, nth element of range}}

where n is basically Range[Length[list]].

The general question here would be how can I create a list from the $a_i$th index of list a, the $b_k$th index of list b and so on.

I would have tried MapThread but I'm trying to use the Histogram3D function so I needed this "nice" list.

share|improve this question
    
Hi, I provided an answer to your question, I'm not quite sure what you mean with the a_i --> b_k list generation. If you have a specific problem with data visualization (e.g. Histogram3D) then you can just ask that question as well. –  Timo Apr 7 '12 at 16:56
add comment

3 Answers

up vote 9 down vote accepted

You can do

Transpose@{ Range@20000 - 1, yourList }

Or more generally if yourList is of varying length

Transpose@{ Range@First@Dimensions@yourList - 1, yourList }

Extracting a part of the list, say from index i to index j can be done with Partor Take

newList = Transpose@{ yourList, Range@First@Dimensions@yourList };
newList[[i;;j]]
(* or *)
Take[newList,{i,j}]
share|improve this answer
    
oh. brilliant. thank you ! didn't see that –  Eiyrioü von Kauyf Apr 7 '12 at 16:49
    
eh.. also! what does '@' do exactly? I see @@ and @@@ under apply, but no @. Edit: found it under 'map' –  Eiyrioü von Kauyf Apr 7 '12 at 16:52
1  
Thank you! That helps. I was wondering what to do about varying length. Do you need to do Range@First@Demensions@yourList -1 though? because doesn't Range give a list which is the same dimension + 1? data = {1,2} Ex. Range[0,Length[data]] --> {0,1,2} if I was to have 0 be the first number that is. –  Eiyrioü von Kauyf Apr 7 '12 at 17:03
    
I missed the starting with 0 bit. You are correct about just substracting one from Range@... The @ notation is equivalent to f[g[a]] === f@g@a. It's just a convenient shorthand to get rid of all those brackets. –  Timo Apr 7 '12 at 19:13
1  
@ is not Map, /@ is. f@x just means f[x] whereas f/@ {x,y,z} does the same as Map[f,{x,y,z}], which yields {f[x],f[y],f[z]}. –  Sjoerd C. de Vries Apr 7 '12 at 22:22
show 1 more comment

I believe that MapIndexed is the most canonical method:

list = {1, 4, 3, 4, 6, 3, 1, 2, 3, 4};

MapIndexed[{#2[[1]] - 1, #} &, list]
{{0, 1}, {1, 4}, {2, 3}, {3, 4}, {4, 6}, {5, 3}, {6, 1}, {7, 2}, {8, 3}, {9, 4}}

However, Transpose and Range is faster when list is a packed array.

share|improve this answer
    
pardon me but ... canonical? What do you mean in that sense? –  Eiyrioü von Kauyf Apr 8 '12 at 20:26
    
@Eiyrioü pardon me if I am abusing terminology. I mean that MapIndexed is the typical, expected, or intended way to accomplish this. It is the built-in function that does exactly this. –  Mr.Wizard Apr 8 '12 at 20:28
    
Interesting I did not know that. Thanks :D. About the terminology I have no clue whether you are or not, I was just wondering if there was some notation/convention I did not know about. Thanks! –  Eiyrioü von Kauyf Apr 8 '12 at 20:41
add comment

If you've just started learning Mathematica, also check out the Table function, which has many applications:

Table[{i - 1, list[[i]]}, {i, 1, 20}]
   {{0, 1}, {1, 4}, {2, 3}, {3, 4}, {4, 6}, ... }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.