2つ以上の同じ要素を持つ列を削除
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
2つ以上の同じ要素を持つ列を削除したいです。
unique関数を使うのでしょうか?
良い手法を教えていただけますと助かります。
before
1 2 3 4 5
2 3 4 4 6 -削除
7 5 5 5 2 -削除
0 9 7 8 1
after
1 2 3 4 5
0 9 7 8 1
0 Kommentare
Akzeptierte Antwort
Atsushi Ueno
am 14 Apr. 2022
before = [1 2 3 4 5; 2 3 4 4 6; 7 5 5 5 2; 0 9 7 8 1]
after = before(all(diff(sort(before'))),:)
all(diff(sort(before')))' % 【参考】ソート⇒差分⇒論理積で、重複の無い行を選択するビット列になる
Weitere Antworten (1)
Akira Agata
am 14 Apr. 2022
arrayfun 使った別の方法:
% 配列の一例 (行・列数は任意)
A = [...
1 2 3 4 5;...
2 3 4 4 6;... % -> 削除
7 5 5 5 2;... % -> 削除
0 9 7 8 1];
% 各行のユニークな要素数
nElement = arrayfun(@(x) numel(unique(A(x,:))), 1:size(A,1));
% ユニークな要素数がAの列数と同じ行インデックスを作成
idx = nElement == size(A,2);
% 対象の行のみ残す
A = A(idx,:);
% 結果を表示
disp(A)
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!