2つ以上の同じ要素を持つ列を削除

30 vues (au cours des 30 derniers jours)
NK
NK le 14 Avr 2022
Commenté : NK le 15 Avr 2022
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

Réponse acceptée

Atsushi Ueno
Atsushi Ueno le 14 Avr 2022
before = [1 2 3 4 5; 2 3 4 4 6; 7 5 5 5 2; 0 9 7 8 1]
before = 4×5
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'))),:)
after = 2×5
1 2 3 4 5 0 9 7 8 1
all(diff(sort(before')))' % 【参考】ソート⇒差分⇒論理積で、重複の無い行を選択するビット列になる
ans = 4×1 logical array
1 0 0 1
  1 commentaire
NK
NK le 15 Avr 2022
シンプルでわかりやすい手法を教えていただきありがとうございました。お陰様で解決しました。

Connectez-vous pour commenter.

Plus de réponses (1)

Akira Agata
Akira Agata le 14 Avr 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)
1 2 3 4 5 0 9 7 8 1
  1 commentaire
NK
NK le 15 Avr 2022
別手法もご提示いただきありがとうございました。こちらも今後の参考にさせていただきます。

Connectez-vous pour commenter.

Catégories

En savoir plus sur データ型の識別 dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!