Find a pair of elements in a 3d matrix

2 次查看(过去 30 天)
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 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by