gpt4 book ai didi

matlab - 查找并用固定的给定行替换具有重复数字的数组的行

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

我有一个矩阵,其中的行有重复的数字。我想找到那些行并将它们替换为虚拟行,以保持矩阵的行数不变。

Dummy_row = [1 2 3]

(5x3) 矩阵 A

A = [2 3 6;
4 7 4;
8 7 2;
1 3 1;
7 8 2]

(5x3) 矩阵 new_A

new_A = [2 3 6;
1 2 3;
8 7 2;
1 2 3;
7 8 2]

我尝试了以下删除具有重复数字的行。

y = [1 2 3]
w = sort(A,2)
v = all(diff(t,1,2)~=0|w(:,1:2)==0,2) % When v is zero, the row has repeated numbers
z = A(w,:)

你能帮忙吗?

最佳答案

bsxfun 基于解决方案-

%// Create a row mask of the elements that are to be edited
mask = any(sum(bsxfun(@eq,A,permute(A,[1 3 2])),2)>1,3);

%// Setup output variable and set to-be-edited rows as copies of [1 2 3]
new_A = A;
new_A(mask,:) = repmat(Dummy_row,sum(mask),1)

代码运行-

A =
2 3 6
4 7 4
8 7 2
1 3 1
7 8 2
new_A =
2 3 6
1 2 3
8 7 2
1 2 3
7 8 2

关于matlab - 查找并用固定的给定行替换具有重复数字的数组的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473652/

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