Search code examples
arraysmatlabmatrixvectorsubmatrix

Submatrix / vector contingent on values in array


I have a 3D matrix A of size 20x500x68. I have two vectors carrying information regarding this matrix which are:
B (containing zeros and ones) of size 1x68 and
C (containing numbers from 1 to 3) of size 1x68
(in length both B and C correspond to the third dimension of A).

I would like to create a "sub matrix" of A only of that third dimension where B==1 and C==3.

Schematically:

[sub matrix of A] = A (B = 1, C = 3)

Is there any way to do this without a loop?


Solution

  • SMA = A(:,:,B==1 & C==3)
    %This submatrix contains all rows and columns of that third dimension of A
    %where B equals 1 and C equals 3