2つ以上の同じ要素を持つ列を削除
32 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Atsushi Ueno
on 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')))' % 【参考】ソート⇒差分⇒論理積で、重複の無い行を選択するビット列になる
More Answers (1)
Akira Agata
on 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)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!