I have a d-dimensional array, A, and vector inds with length equal to d. I would like to access the value of A at inds.
Ideally, I'd do something like A(*inds) (borrowing the unpacking syntax from Python). I'm not sure how to do this in MATLAB.
If I do A(inds) I actually get d separate values from A, which is not what I want. What I want is for element i of inds to be the ith parameter in the function call A().
One solution is to create a comma-separated list out of your vector of subscripted indices
inds
. You can do this by converting it to a cell array using NUM2CELL, then using the{:}
syntax when indexingA
:I think this tool might help you:
if you have an ND matrix like R = rand(5,10,15,20), and you want to access elements in a particular pattern, you can use spindex to access the output in the same shape as the input access. So if you have size(i1) = [5,5,5], size(i2) = [5,5,5], etc. Then size(spindex(R,i1,i2,i3,i4)) also equals [5,5,5].
you will need to add the code below as spindex.m to a location in your MATLAB path.