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.

In my work I found that I needed the Extract function. However I was wondering if there was a way to use Part. Assume that mList is a 4-dim array.

I did the following, which worked.

x = {2, 10, 3, 4};
y = Extract[mList, x];

I found that I can't do this.

y = mList[[x]]

Is there a way to convert the list x, into a sequence of integers that I can use in Part? I tried enclosing it in Sequence, but that seemed to have no effect.

As I said, I'm fine without this, I was just wondering.

share|improve this question

3 Answers 3

up vote 6 down vote accepted
SeedRandom@1;
myList = RandomInteger[{0, 10}, {10, 10, 10, 10}];
x = {2, 10, 3, 4};
y = Extract[myList, x]
9
Part[myList, Sequence @@ x]
9

On a smaller list:

list = {{a, b}, {c, d}};
{Extract[list, {1, 1}], Part[list, Sequence @@ {1, 1}]}
{a, a}

Knowing that, as Kuba noted, Part[list, Sequence @@ {1, 1}] is equivalent to list[[##]] & @@ {1, 1}.

share|improve this answer

Two further variations of the same theme (actually for the first one only the input syntax is different):

myList[[Sequence@@x]]
mylist[[##&@@x]]

And a different one:

Part@@Prepend[x,myList]
share|improve this answer
    
The first 2 in particular and very nice! The 3rd was very instructional. –  Mitchell Kaplan Jun 13 at 14:38

Try this also:

SeedRandom@1;
myList = RandomInteger[{0, 10}, {10, 10, 10, 10}];
y=myList[[Delete[x, 0]]]

or

y=myList[[x /. List -> Sequence]]
share|improve this answer

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.