Find values of a specific page of a 3d matrix at specific positions

1 view (last 30 days)
I have two matrices (A and B). A is a 2D matrix (3x3) which defins some zones (1, 2, and 3).
A=
1 1 1
2 2 2
3 3 3
B is a 3D matrix (3x3x2) like:
B(:,:,1)
1 2 1
2 1 2
1 1 2
B(:,:,2)
2 2 2
1 1 2
2 2 1
I would like to find the indices (rows, and columns, or linear indices) of B, when B equals (e.g.) 2 only in (e.g.) page 1, and only in (e.g.) zone 2.
I tried this, but it didn't do the job:
[rA_2 cA_2]= find(A==2);
[r0B_2 c0B_2]= ind2sub(size(B),find(B(rA_2,cA_2,1)==2);
I seem not to find a solution for what I thought to be a quite simple task.

Accepted Answer

Adam
Adam on 15 Apr 2015
Edited: Adam on 15 Apr 2015
pageIdx = find( B(:,:,pageNo) == 2 & A == zoneNo );
idx = pageIdx + 9 * ( pageNo - 1 );
should work I think. First find the indices on the chosen page, then add the indices for all previous pages to that (i.e. there are 9 indices per page previous to the chosen one).
  2 Comments
Umberto Minora
Umberto Minora on 15 Apr 2015
Edited: Umberto Minora on 15 Apr 2015
Yep, it did the job. Thanks! Anyway, for a more generic use, I woul do idx = pageIdx + numel(A)*(pageNo-1);
Moreover, to find rows and columns (instead of linear indices) one would do [r c]= ind2sub(size(B),pageIdx+numel(A)*(pageNo-1))
Adam
Adam on 15 Apr 2015
Well if you only want rows and columns on the given page then you can ignore all the stuff with adding page numbers and just use:
[r, c] = find( B(:,:,pageNo) == 2 & A == zoneNo );

Sign in to comment.

More Answers (0)

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!