3
$\begingroup$

This question already has an answer here:

Suppose I have a list l which contains the indices of some array a. For instance,

l = {{1,1},{1,2}}
a = {{a11,a12},{a21,a22}}

I want to access the elements of a at the positions given by l, i.e. in this case I want to access

a[[1,1]] = a11 
a[[1,2]] = a12

However, I cannot simply feed the elements of l into the a with double brackets:

In[1]:= a[[{1, 1}]]
Out[2]= {{a11, a12}, {a11, a12}}

Is there an easy way to remove the curly brackets in the entry of the list? It should be generalizable to much bigger arrays (so not something like a[[l[[1,1]],l[[1,2]]]] = a11).

$\endgroup$

marked as duplicate by Kuba list-manipulation Sep 20 '16 at 14:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Extract might be a useful alternative. $\endgroup$ – Karsten 7. Sep 20 '16 at 14:29
1
$\begingroup$

I think Sequence does the job for you:

a[[Sequence @@ l[[1]]]]
(* a11 *)

Equivalently, without (most) brackets, call it Part:

Part[a, Sequence @@ l[[1]]]
(* a11 *)
$\endgroup$

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