我的矩阵有重复数字的行.我想找到那些行并用虚拟行替换它们,以便保持矩阵的行数不变.
Dummy_row = [1 2 3]
Run Code Online (Sandbox Code Playgroud)
(5x3)矩阵A.
A = [2 3 6;
4 7 4;
8 7 2;
1 3 1;
7 8 2]
Run Code Online (Sandbox Code Playgroud)
(5x3)Matrix new_A
new_A = [2 3 6;
1 2 3;
8 7 2;
1 2 3;
7 8 2]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下删除了重复数字的行.
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,:)
Run Code Online (Sandbox Code Playgroud)
你能帮忙吗?
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)
Run Code Online (Sandbox Code Playgroud)
代码运行-
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
Run Code Online (Sandbox Code Playgroud)