Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Accessing an element of a multidimensional array with a list

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().

like image 277
emchristiansen Avatar asked Jun 17 '11 19:06

emchristiansen


People also ask

How do you access elements in a multidimensional array?

An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array. int x = a[1,1]; Console.

How do you access elements of an array in MATLAB?

To access elements in a range of rows or columns, use the colon . For example, access the elements in the first through third row and the second through fourth column of A . An alternative way to compute r is to use the keyword end to specify the second column through the last column.

Can MATLAB handle multidimensional arrays?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

How do you concatenate a list in MATLAB?

You can use the square bracket operator [] to concatenate or append arrays. For example, [A,B] and [A B] concatenates arrays A and B horizontally, and [A; B] concatenates them vertically.


1 Answers

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 indexing A:

inds = num2cell(inds);
value = A(inds{:});
like image 92
gnovice Avatar answered Sep 25 '22 21:09

gnovice