Re: [問題] 矩陣合併與刪除part2已回收
寫了個通用版的
應該有其他更好的寫法
程式碼如下:
%====================================
clc
clear
x = [...
1 4 6
1 3 9
1 2 3
2 3 4
3 4 5];
y = [...
1 2 3
1 5 4
2 3 4
4 5 6
5 5 3];
count = 1;
Num = unique(x(:,1));
Len = length(Num);
Colx = size(x,2);
Coly = size(y,2);
for n = 1:Len
Locx = find(x(:,1)==Num(n));
Locy = find(y(:,1)==Num(n));
Lenx = length(Locx);
Leny = length(Locy);
LenMin = min(Lenx,Leny);
if LenMin~=0
z(count:count+LenMin-1,1) = Num(n);
z(count:count+LenMin-1,2:Colx) = x(Locx(1:LenMin),2:end);
z(count:count+LenMin-1,Colx+1:Colx+Coly-1) = y(Locy(1:LenMin),2:end);
count = count+LenMin;
end
end
%====================================
執行結果:
z =
1 4 6 2 3
1 3 9 5 4
2 3 4 3 4
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.207.32
→
07/26 22:12, , 1F
07/26 22:12, 1F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):