Find a pair of elements in a 3d matrix

1 Ansicht (letzte 30 Tage)
Efstathios Kontolatis
Efstathios Kontolatis am 6 Okt. 2016
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?

Akzeptierte Antwort

Thorsten
Thorsten am 6 Okt. 2016
Bearbeitet: Thorsten am 6 Okt. 2016
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);

Weitere Antworten (1)

Giovanni Mottola
Giovanni Mottola am 6 Okt. 2016
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.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by