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.
Answer
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}.
Comments
Post a Comment