简体   繁体   English

MATLAB:在多维数组中查找值的坐标

[英]MATLAB: Finding coordinates of value in multidimensional array

I have a three-dimensional array, and I'd like to be able to find a specific value and get the three coordinates. 我有一个三维数组,我希望能够找到一个特定的值并获得三个坐标。

For example, if I have: 例如,如果我有:

A = [2 4 6; 8 10 12]

A(:,:,2) = [5 7 9; 11 13 15]

and I want to find where 7 is, I'd like to get the coordinates i = 1 j = 2 k = 2 我想找到7位置,我想得到坐标i = 1 j = 2 k = 2

I've tried variations of find(A == 7) , but I haven't got anywhere yet. 我尝试过各种变体find(A == 7) ,但我还没到任何地方。

Thanks! 谢谢!

The function you seek is ind2sub : 你寻求的功能是ind2sub

[i,j,k]=ind2sub(size(A), find(A==7))
i =
     1
j =
     2
k =
     2

You can use find to locate nonzero elements in an array, but it requires a little bit of arithmetic. 您可以使用find在数组中定位非零元素,但它需要一些算术运算。 From the documentation: 从文档:

[row,col] = find(X, ...) returns the row and column indices of the nonzero entries in the matrix X. This syntax is especially useful when working with sparse matrices. [row,col] = find(X, ...)返回矩阵X中非零项的行索引和列索引。在使用稀疏矩阵时,此语法特别有用。 If X is an N-dimensional array with N > 2, col contains linear indices for the columns. 如果X是N> 2的N维数组,则col包含列的线性索引。 For example, for a 5-by-7-by-3 array X with a nonzero element at X(4,2,3), find returns 4 in row and 16 in col. 例如,对于在X(4,2,3)处具有非零元素的5×7×3阵列X,find返回行中的4和col中的16。 That is, (7 columns in page 1) + (7 columns in page 2) + (2 columns in page 3) = 16. 即,(第1页中的7列)+(第2页中的7列)+(第3页中的2列)= 16。

If the matrix M has dimensions axbxc , then the indices (i,j,k) for some value x are: 如果矩阵M具有维度axbxc ,那么某些值x的索引(i,j,k)是:

[row,col] = find(A==x);
i = row;
j = mod(col,b);
k = ceil(col/b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM