gpt4 book ai didi

matlab - 检查数据是否存在于另一个矩阵中,如果存在,则替换它

转载 作者:行者123 更新时间:2023-12-02 03:34:29 25 4
gpt4 key购买 nike

在 Octave 中,有这样的矩阵:

abc = [1 2 3; 5 7 8];
def = [5 7 9; 10 11 12];

我想要一个函数,对于 abc 中的所有 [x y z1],检查 def 中是否存在 [x y z2],如果存在,则替换它。

所以,我想要类似的东西

checkandreplace(abc, def);

将 abc 更改为 [1 2 3; 5 7 9]

编辑:

我能想出的最好办法是循环方案:

for i = 1:size(abc, 1)
index = find(ismember(def(:, [1 2]), abc(i, [1 2]), 'rows'))
if(index)
abc(i, :) = def(index, :)
endif
endfor

可以用更好的方式完成吗?

编辑:

忘记补充说它应该是稳定的,即它不应该改变 abc 中行的顺序。

感谢您的帮助!

最佳答案

您可以通过一次调用 ismember 来完成所有事情:

[m,I] = ismember(abc(:,1:2), def(:,1:2), 'rows');
abc(m,:) = def(I(m),:);

关于matlab - 检查数据是否存在于另一个矩阵中,如果存在,则替换它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524363/

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