Find a pair of elements in a 3d matrix

1 回表示 (過去 30 日間)
Efstathios Kontolatis
Efstathios Kontolatis 2016 年 10 月 6 日
回答済み: Giovanni Mottola 2016 年 10 月 6 日
I have a 512*512*2 matrix. If A is the matrix then I want to find the pair of elements A(:,:,1) and A(:,:,2) that are equal to a specific pair. For example I want to check if the A(1,1,1) and A(1,1,2) are equal to (0,0) and if so to keep the position (1,1). Is there a way to do so?

採用された回答

Thorsten
Thorsten 2016 年 10 月 6 日
編集済み: Thorsten 2016 年 10 月 6 日
Logical index:
idx = A(:,:,1) == 0 & A(:,:,2) == 0;
if you prefer a single index, you can use
idx2 = find(idx);
if you prefer multiple subscripts as index, you can use
[ind_i, ind_j] = ind2sub(size(A), idx2);
  1 件のコメント
Efstathios Kontolatis
Efstathios Kontolatis 2016 年 10 月 6 日
Thanks a lot

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

その他の回答 (1 件)

Giovanni Mottola
Giovanni Mottola 2016 年 10 月 6 日
Note: if it's 512*512*2 (three dimensional), it's called tensor, not matrix.
A way to do what you require would be to first define the two values you're looking for:
val1=0;
val2=0;
Then call:
[row, col]=find(A(:, :, 1)==val1 & A(:, :, 2)==val2)
Example with a smaller matrix: let
A(:,:,1) =
4 0 5 0 3
1 4 0 0 10
2 5 8 10 3
7 8 4 2 0
7 1 2 10 6
A(:,:,2) =
6 10 2 3 2
9 9 5 5 7
2 7 1 0 10
8 2 9 3 6
3 0 10 5 2
The pair we're looking for is, say, val1=4 and val2=9. Using the command above, we get
row =
2
4
col =
2
3
which can be easily checked.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by