Reputation: 890
In Python numpy it is possible to use arrays of indexes, as in (taken from the tutorial):
data = array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
i = array( [ [0,1], # indices for the first dim of data
[1,2] ] )
j = array( [ [2,1], # indices for the second dim
[3,3] ] )
Now, the invocation
data[i,j]
returns the array
array([[ 2, 5],
[ 7, 11]])
How can I get the same in Matlab?
Upvotes: 1
Views: 285
Reputation: 45752
I think you will have to use linear indexing which you'll get from the sub2ind
function like this:
ind = sub2ind(size(data), I,J)
example:
data =[ 0, 1, 2, 3
4, 5, 6, 7
8, 9, 10, 11]
i = [0,1;
1,2];
j = [2,1;
3,3]
ind = sub2ind(size(data), i+1,j+1);
data(ind)
ans =
2 5
7 11
notice that I went i+1
and j+1
, this is because unlike Python which starts indexing at 0
, Matlab starts indexing from 1
.
Upvotes: 3