フィルターのクリア

use of find command for 3d array

22 ビュー (過去 30 日間)
Hall marokey
Hall marokey 2017 年 3 月 15 日
コメント済み: Guillaume 2017 年 3 月 15 日
How to use find command for a 3d array
eg. A = rand(5,5,3)
I want to get i,j,k location of the array A for value [0.5 ,0.2,0.3]
psuedo code
[i,j,k] = find(A(:,:,1) == .5 & A(:,:,2)== 0.2 & A(:,:,3) ==0.3)
but this is not working any help is highly appreciated

採用された回答

Guillaume
Guillaume 2017 年 3 月 15 日
編集済み: Guillaume 2017 年 3 月 15 日
For arrays with more than two dimensions you have to get the linear index (1 output version of find) and then transform that into ND indices using ind2sub:
idx = find(yourlogicalarray);
[row, col, page] = ind2sub(size(yourlogicalarray), idx);
However, in your example the array you're passing to find is a 2D array, not 3D. So, in your case:
[row, col] = find(A(:,:,1) == .5 & A(:,:,2)== 0.2 & A(:,:,3) ==0.3);
is all that is required. And to get the 3 values that match these coordinates:
A(row, col, :)
  2 件のコメント
Jan
Jan 2017 年 3 月 15 日
編集済み: Jan 2017 年 3 月 15 日
+1. A marginal note: Some years ago I asked the support if expressions like ".5" and "5." are guaranteed to work and if this is documented. The answer was: "We don't know. Everything, which can be created by sprintf, is supported. Stay at this." All interpreters I know accept ".5" so I think this is just a question of taste - but not documented. :-)
Guillaume
Guillaume 2017 年 3 月 15 日
To be honest, I just copy/pasted the original code for that bit. Didn't even notice the .5 (and the inconsistent use of blank spaces).
That the answer is "We don't know" is alarming! I would expect the grammar of the parser to be well defined, not left to random chance. As far as I know, both are perfectly fine.

サインインしてコメントする。

その他の回答 (1 件)

Adam
Adam 2017 年 3 月 15 日
doc ind2sub
Use the standard form of find that returns linear indices and convert them using ind2sub to 3d.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by