我正在使用矩阵和面具。我用魔法(3)尝试了它,所以我的矩阵是:
8 1 6;3 5 7;4 9 2
。我还有一个0 0 0;1 0 1;0 0 1
的面具。
现在我做find(mask==1)
。所以我得到掩码= mask == 1。
但是从现在开始,我被困住了。我想像这样改变矩阵。
在返回的find(mask ..)的所有索引中,我想检查我的矩阵的值是否具有某个值,如果不是,则将其设置为0.
你能帮帮我吗?
编辑:假设如果掩码为1的矩阵值低于4,则将它们更改为零。结果应为[8 0 6; 0 5 7; 4 9 0]
;
答案 0 :(得分:3)
如果我理解正确,解决方案很简单:
A = magic(3); %//Example matrix
mask = A<4; %//Example mask
A(mask)=[]; %//Element removal. Risky if reshape is required later!
%// or A(mask)=0;
%// or A(mask)=NaN;
修改:此解决方案回答了一个稍微修改过的问题,正如评论中的OP所阐明的那样。
答案 1 :(得分:2)
我会这样做:
A = [8 1 6;3 5 7;4 9 2];
mask = [0 0 0;1 0 1;0 0 1];
%// To know your elements of A greater than the desired value
A_indexes = A > threshold;
%// To get the mask values (note: if your mask is always
%// binary, this step is not needed)
mask_indexes = mask == 1;
%// The elements you want to modify
indexes = A_indexes & mask_indexes;
%// Finally the modification
A(indexes) = 0;