2つ以上の同じ要素を持つ列を削除
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
Accepted Answer
More Answers (1)
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)
Categories
Find more on Shifting and Sorting Matrices 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!