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개)

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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by