indexing - How can I index a 3-D matrix with a 2-D mask in MATLAB? -
Suppose I have D, an X-by-Y-by-Z data matrix. I have an X-by-Y "masking" matrix, I have to set my target elements (Xi, Yi, :) in D to N, when X in X (Xi, Yi) is false.
Is there any way to avoid doing this in a loop? I tried to use ind2sub
, but it fails:
M = logical (round (rand (3,3)); % Mask D = randn (3,3,2); Try liding the combinations of% data% x, y elements [x, y] = ind2sub (size (m), find (m == 0)); D_masked = D; D_masked (x, y, :) = NaN; % Does not work! % This is the old method D_masked = D; For IX = 1: size for iy = 1 (m, 1): size (m, 2) if ~ m (ix, ii), d_maskad (iX, ii, :) = nyan; End and end
I suspect that I am clarifying something here (
You can do this by filling your logical mask «code> M The dimension is so much that it is the same size as D
. Then, remove the index:
D_masked = D; D_masked (repmat (~ m, [1 size] If the mask Replacing the matrix is undesirable. Then you can search for a set of linear index where
Copy the bar, thenM, where
is equal to 0, then set that set to < Code> Size (D, 3)numel (M)
Set multiple indexes from one to the other so that it is indexed and a different part ofD
in the third dimension. I will explain this by using:D_masked = D; index = Bsxfan (@ plus, find (~ m), (0: (size (d, 3) -1). * Digit (m)); d_masked (index) = nan;
Comments
Post a Comment