gpt4 book ai didi

matlab - 如何在 MATLAB 中将两个掩码合并在一起?

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:48 24 4
gpt4 key购买 nike

我有两个掩码,我想合并在一起,用 mask2 覆盖 mask1 除非 mask2 有一个零。掩码不是二进制的,它们是用户在感兴趣区域定义的某个值,其他地方为 0。

例如如果:

mask1=[0 5 5;0 5 5];
mask2=[4 4 0;4 4 0];

然后我想要一个 [4 4 5;4 4 5] 的输出。如果我再戴一个面具,

mask3=[0 6 0;0 6 0];  

然后我想要输出 [4 6 5;4 6 5]

必须有一种简单的方法可以做到这一点,而无需遍历和比较矩阵中的每个元素。时间很重要,因为矩阵非常大,我需要合并很多矩阵。任何帮助都会很棒。

最佳答案

另一种选择是使用 logical indexing :

%# Define masks:

mask1 = [0 5 5; 0 5 5];
mask2 = [4 4 0; 4 4 0];
mask3 = [0 6 0; 0 6 0];

%# Merge masks:

newMask = mask1; %# Start with mask1
index = (mask2 ~= 0); %# Logical index: ones where mask2 is nonzero
newMask(index) = mask2(index); %# Overwrite with nonzero values of mask2
index = (mask3 ~= 0); %# Logical index: ones where mask3 is nonzero
newMask(index) = mask3(index); %# Overwrite with nonzero values of mask3

关于matlab - 如何在 MATLAB 中将两个掩码合并在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1775866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com