How to remove rows having repeating elements from a matrix?

1 回表示 (過去 30 日間)
Susmita
Susmita 2016 年 4 月 25 日
コメント済み: David Wagener 2019 年 12 月 1 日
If I have a matrix A=[ 1 2 1 4; 1 2 3 4; 3 2 1 4; 3 3 2 1] then I want a matrix as B=[ 1 2 3 4; 3 2 1 4] so that both the 1st and 4th row will be deleted as they have repeated elements. pls help me to find out the code.

採用された回答

Roger Stafford
Roger Stafford 2016 年 4 月 25 日
編集済み: Roger Stafford 2016 年 4 月 27 日
t = false(1,size(A,1));
for k = 1:size(A,1)
u = unique(A(k,:));
t(k) = size(u,2)==size(A,2);
end
B = A(t,:); % Rows with repeated elements deleted
  1 件のコメント
Susmita
Susmita 2016 年 4 月 27 日
Thanks for d answer...It's running correctly..

サインインしてコメントする。

その他の回答 (1 件)

Baltam
Baltam 2016 年 4 月 25 日
The below method is not elegant but it works.
A=[ 1 2 1 4; 1 2 3 4; 3 2 1 4; 3 3 2 1];
index = zeros(1,size(A,1));
for i = 1:size(A,1)
if numel(unique(A(i,:))) == numel(A(i,:))
index(i)=1;
end
end
index = logical(index);
B = A(index,:);
Kind regards,
Baltam
  2 件のコメント
Susmita
Susmita 2016 年 4 月 27 日
This code is also running correctly..thanks@Baltam
David Wagener
David Wagener 2019 年 12 月 1 日
The second version shown here while not as elegant works better if you have a large matrix and are using parallel processing. I'm not an expert by any means but the second version did not overload my machine like the first one did.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by