开发者

How can I index a 3-D matrix with a 2-D mask in MATLAB?

开发者 https://www.devze.com 2023-01-10 10:06 出处:网络
Suppose I have D, an X-by-Y-by-Z data matrix. I also have M, an X-by-Y \"masking\" matrix. My goal is to set the elements (Xi,Yi,:) in D to NaN when (Xi,Yi) in M is false.

Suppose I have D, an X-by-Y-by-Z data matrix. I also have M, an X-by-Y "masking" matrix. My goal is to set the elements (Xi,Yi,:) in D to NaN when (Xi,Yi) in M is false.

Is there any way to avoid doing this in a loop? I tried using ind2sub, but that fails:

M = logical(round(rand(3,3))); % mask
D = randn(3,3,2); % data

% try getting x,y pairs of elements to be masked
[x,y] = ind2sub(size(M),find(M == 0));
D_masked = D;
D_masked(x,y,:) = NaN; % does not work!

% do it the old-fashioned way
D_masked = D;
for iX = 1:size(M,1)
    for iY = 1:size(M,2)
        if ~M(iX,iY), D_masked(iX,iY,:开发者_运维百科) = NaN; end
    end
end

I suspect I'm missing something obvious here. (:


You can do this by replicating your logical mask M across the third dimension using REPMAT so that it is the same size as D. Then, index away:

D_masked = D;
D_masked(repmat(~M,[1 1 size(D,3)])) = NaN;

If replicating the mask matrix is undesirable, there is another alternative. You can first find a set of linear indices for where M equals 0, then replicate that set size(D,3) times, then shift each set of indices by a multiple of numel(M) so it indexes a different part of D in the third dimension. I'll illustrate this here using BSXFUN:

D_masked = D;
index = bsxfun(@plus,find(~M),(0:(size(D,3)-1)).*numel(M));
D_masked(index) = NaN;


Reshape is basically for free, you can use it here for an efficient solution. reducing the whole to a 2d problem.

sz=size(D);
D=reshape(D,[],sz(3)); %reshape to 2d
D(isnan(M(:)),:)=nan; %perform the operation on the 2d matrix
D=reshape(D,sz); %reshape back to 3d


My Matlab is a bit rusty but I think logical indexing should work:

D_masked = D;
D_masked[ M ] = NaN;

(which probably can be combined into one statement with a conditional expression on the rhs...)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号