Find the inear indices of a specific value in a 3d matrix using the positions of a 2d matrix

1 view (last 30 days)
If I have one 3d matrix A, and one 2d matrix B, how can I find the linear indices of a specific value of A in all its pages, given the position of another value of B?
For instance, if A is a 3x4x4 matrix like:
A(:,:,1)=
0 1 2 0
1 1 0 2
2 2 0 1
A(:,:,2)=
2 0 0 0
0 1 0 2
2 0 1 1
and B is a 3x4 one:
1 1 1 1
2 2 2 2
3 3 3 3
I would like to find all the linear indices of A == 2 when B == 1, such as:
LinInd= [7; 13]
How can I do that?

Accepted Answer

Guillaume
Guillaume on 15 Apr 2015
Edited: Guillaume on 15 Apr 2015
Hum, that is a bit tricky indeed. One possible way to solve it would be to repmat the second conditions across the pages of A:
find(A == 2 & repmat(B == 1, [1 1 size(A, 3)]))
edit: Not sure where you get your [7;10] result. I get [7;13]

More Answers (1)

James Tursa
James Tursa on 15 Apr 2015
Another way:
find(bsxfun(@and,A==2,B==1))

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!