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

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

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]

1 Comment

I edited my question. Indeed, it was [7;13], I was wron in counting the positions. Thanks a lot, it worked!

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!