MATLABのコードって忘れやすい・・・

プログラムのコマンドなんて忘れやすいもの・・・いっそのことネット上に保存してシェアしましょう!!
It's easy to forget command for MATLAB in programming.... Let's share small help for Matlab code with you on the web!!



2011年6月14日火曜日

重複するペアーを削除 pairfind

こんな関数作ってみました。
temp1は2行X列の行列。縦のペアが他の列で重複するものがある場合、
検索して自動的に削除してくれます。

function temp=pairfind(temp1)
% Remove overlapped pair.
% ---Input---
% temp1: matrix(2 x X)
%
temp=temp1;
for k=1:size(temp,2)-1,
idx=[];
for kk=k+1:size(temp,2),
if((temp(1,k)==temp(1,kk))&&(temp(2,k)==temp(2,kk))),
idx=[idx kk];
%disp('bingo!')
end
end
temp(:,idx)=[];
end
%%%%%% END %%%%%%%%%%%%

(例)
AA=[1 2 3 1 2 3 1 2 3 1 2 3;
1 1 1 1 2 2 2 2 3 3 3 3];

rA=pairfind(AA)

rA =

1 2 3 2 3 1 3 1 2
1 1 1 2 2 2 3 3 3