在Python中,可以使用索引数组,如(摘自本教程):
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] ] )
现在,调用
data[i,j]
返回数组。
array([[ 2, 5],
[ 7, 11]])
我怎么能在Matlab中得到同样的信息呢?
发布于 2014-03-19 11:59:09
我认为您必须使用线性索引,这将从sub2ind
函数中得到如下所示:
ind = sub2ind(size(data), I,J)
示例:
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
请注意,我使用了i+1
和j+1
,这是因为与0
开始索引不同,Matlab从1
开始索引。
https://stackoverflow.com/questions/22505041
复制相似问题