两个数组定义二维坐标,如数组索引,在MATLAB /八度 [英] two arrays defining 2d coordinates, as array indices, in matlab/octave
问题描述
我有一个二维数组,称之为A。我有两个其他二维数组,称他们为九和IY。我想创建一个输出数组,其元素是A在由x_idx和y_idx提供的指数对的元素。我可以用一个循环做到这一点如下:
I have a 2D array, call it 'A'. I have two other 2D arrays, call them 'ix' and 'iy'. I would like to create an output array whose elements are the elements of A at the index pairs provided by x_idx and y_idx. I can do this with a loop as follows:
for i=1:nx
for j=1:ny
output(i,j) = A(ix(i,j),iy(i,j));
end
end
我怎么能做到这一点不循环?如果我做输出= A(IX,IY),我得到一个值的整个范围内(九)X(IY)。
How can I do this without the loop? If I do output = A(ix,iy), I get the value of A over the whole range of (ix)X(iy).
感谢您,
杰森
Thank you, Jason
推荐答案
一个更快的方法是使用线性索引直接,而无需调用SUB2IND:
A faster way is to use linear indexing directly without calling SUB2IND:
output = A( size(A,1)*(iy-1) + ix )
...想矩阵A为一维数组(列方式排列)
... think of the matrix A as a 1D array (column-wise order)
这篇关于两个数组定义二维坐标,如数组索引,在MATLAB /八度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!