MATLAB: I have a 2400×2400 matrix that I would like to use as a mask for each layer of a 2400x2400x46 matrix. Since the matrix dimensions are not equal, I cannot do so by simply multiplying the mask matrix by the larger matrix. How might I do this

applying 'mask' matrices to matrix of unequal dimensions

I have a 2400×2400 matrix that I would like to use as a mask for each layer of a 2400x2400x46 matrix. Since the matrix dimensions are not equal, I cannot do so by simply multiplying the mask matrix by the larger matrix. How might I do this? The end goal is to have a 2400x2400x46 matrix that has "NaN" for all "NaN" values in the mask matrix.
Thank you.

Best Answer

% Mask the image.
maskedHyperSpectralImage = bsxfun(@times, HyperSpectralImage, cast(mask,class(HyperSpectralImage)));
Note: the above is all one line of code.