5
$\begingroup$

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.

$\endgroup$
8
$\begingroup$
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}.

$\endgroup$
5
$\begingroup$

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]
$\endgroup$
  • $\begingroup$ The first 2 in particular and very nice! The 3rd was very instructional. $\endgroup$ – Mitchell Kaplan Jun 13 '14 at 14:38
4
$\begingroup$

Try this also:

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

or

y=myList[[x /. List -> Sequence]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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