Here's a simple concrete example with a rank-3 tensor:
In[19]:= s = RandomReal[1, {5, 6, 7}];
Dimensions[s]
TensorRank[s]
Out[20]= {5, 6, 7}
Out[21]= 3
Extracting the element at position {3, 1, 2}
can be done by repeatedly taking a Part
([[...]]
) each level down, but it's easier to just use a single Part
spec:
In[35]:= s[[3]][[1]][[2]]
s[[3, 1, 2]]
Out[35]= 0.350807
Out[36]= 0.350807
If you have the position stored in a variable, you can use Sequence
to splice it into the Part
brackets or you can use extract:
In[26]:= t = {3, 1, 2};
s[[Sequence @@ t]]
Extract[s, t]
Out[27]= 0.350807
Out[28]= 0.350807
If you really want to use repeated parts (which is probably not a good idea, but just for purposes of illustration), you can use Fold
, to repeatedly extract the next level of the tensor. This is equivalent to s[[3]][[1]][[2]]
:
In[32]:= Fold[#1[[#2]] &, s, t]
Out[32]= 0.350807
s[[t]]
. $\endgroup$s
hasm
indices, right? So basically you wants[[t1, t2, t3, ..., tm]]
if I understand correctly. That could be achieved bys[[Sequence @@ t]]
orExtract[s, t]
$\endgroup$