3D indices for particular values in a 3D array

29 views (last 30 days)
I want to find the 3D indices for particular values in a 3D array. I was expecting to use something like one would use for a 2D array:
>>[row,col] = find(a==8)
So I tried
>>[row,col,page] = find(a==8)
and
>>[row,col,sheet] = find(a==8)
but these just gave nonsense. Surely there is a way to do this?
W.Sherwin@unsw.edu.au

Accepted Answer

Stephen23
Stephen23 on 8 Apr 2024 at 7:52
Edited: Stephen23 on 8 Apr 2024 at 8:31
Use IND2SUB:
X = randi(9,4,3,2)
X =
X(:,:,1) = 9 4 1 3 1 8 5 3 7 8 8 4 X(:,:,2) = 7 4 7 7 9 3 6 3 9 8 2 7
[R,C,P] = ind2sub(size(X),find(X==8))
R = 4x1
4 4 2 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = 4x1
1 2 3 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
P = 4x1
1 1 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 Comments
Stephen23
Stephen23 on 9 Apr 2024 at 4:48
@William Sherwin: please remember to click the accept button of the answer that helped you!

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!