How to remove repeating elements from an array

357 ビュー (過去 30 日間)
NS
NS 2011 年 9 月 25 日
コメント済み: Life is Wonderful 2020 年 4 月 12 日
Hi Guys,
I have two matrices X and Y of the same size. Each element in Y has a corresponding element in X. There are some repeating values in X (and correspondingly in Y as well). Is there any way to remove these elements from the matrices.
Thanks, NS
  2 件のコメント
Hesham Hendy
Hesham Hendy 2014 年 11 月 22 日
If I want to keep one value of repeated values without sorting, what can I do ???
Image Analyst
Image Analyst 2014 年 11 月 22 日
Start a new question and give a small example.

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

採用された回答

UJJWAL
UJJWAL 2011 年 9 月 26 日
Hi NS,
Ok. Now I understand what you want. ok. There is a way to do this. Take a look at the code below :
a = randi(10,[1,20]);
[b,m1,n1] = unique(a,'first');
[c1,d1] =sort(m1);
b = b(d1);
In this way your problem is solved. similarly you can also do so for the other matrix. Hope this helps...
Happy to help
UJJWAL
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 9 月 26 日
[a b] = unique([X;Y]','rows','first');
xyb = sortrows([a b],3)';
X = xyb(1,:);
Y = xyb(2,:);
NS
NS 2011 年 9 月 26 日
Thanks. I figured it that day itself. :)

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

その他の回答 (4 件)

UJJWAL
UJJWAL 2011 年 9 月 25 日
Use the unique function. Take the following example :-
a = randi(10,[1,20]) will display a 1 by 20 matrix of pseudorandom integers with uniform distribution in the range 1:10 . Obviously there will be repeated elements in the matrix. Suppose now you create a second matrix p = sin(a). So there is a mapping defined. Now type unique(a). It will give you only the unique values in a that is it will not display repeated values of the same element. Now type unique(p) : It will similarly display the unique values in p.
Hope it resolves your doubt
Best Wishes
Ujjwal
  2 件のコメント
Jan
Jan 2011 年 9 月 25 日
If you want to remove elements on the same positions in both matrices, you can use the 2nd and 3th output of unique. See "help unique".
NS
NS 2011 年 9 月 25 日
Hi Ujjwal,
I dont want the order of the elements to be rearranged. When I use unique, the elements are arranged in an ascending order.
Eg. if X=[0 13 18 22 22] and Y=[0 1 -1 3 3]
I want my output to be as X=[0 13 18 22] and Y=[0 1 -1 3]
How do I do that

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


Fangjun Jiang
Fangjun Jiang 2011 年 9 月 26 日
For duplicated x-y pairs, if you want to keep the last one:
X=[0 13 22 18 22 22 10];
Y=[0 1 3 -1 3 3 -1];
A=[X;Y]';
[UniXY,Index]=unique(A,'rows');
DupIndex=setdiff(1:size(A,1),Index);
A(DupIndex,:)=[];
X=A(:,1)'
Y=A(:,2)'
X =
0 13 18 22 10
Y =
0 1 -1 3 -1
If you want to keep the first one, use:
[UniXY,Index]=unique(A,'rows','first');
  1 件のコメント
Aidin Golrokh
Aidin Golrokh 2018 年 7 月 18 日
For a big matrix, this code will produce repeating numbers.

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


SIVAKUMAR KARURNKARAN
SIVAKUMAR KARURNKARAN 2015 年 5 月 20 日
function B=adjacentmat(n,e) R=combntns(1:n,2); k=1:e; r=nchoosek(n,2); B=zeros(n,n,r,r);
for i=1:r for j=1:r R([i j],:)=R([j i],:); F=R(k,:); A=zeros(n,n); for m =1:e; A(F(m,1),F(m,2))=1; A(F(m,2),F(m,1))=1;
end
B(:,:,i,j)=A;
end
end
return in this output has more duplicate matrices i want only distinct matrices. i need the code of comparing the matrices and find which is distinct

Ismet Ozturk
Ismet Ozturk 2018 年 3 月 29 日
編集済み: Ismet Ozturk 2018 年 3 月 29 日
function [ a ] = deleteRepetation( a )
leng=size(a);tempa=zeros(1,2);
for i=1:leng(1)
tempa=a(i,:);
tempa=sort(tempa);
a(i,:)=tempa;
end
deletionList=[1];
while size(deletionList)>0
deletionList=[];leng=size(a);
for i=1:leng(1)
tempa=a(i,:);
for j=i+1:leng(1)
if tempa==a(j,:)
deletionList=[deletionList j];
end
end
end
a(deletionList,:)=[];
end
  1 件のコメント
Life is Wonderful
Life is Wonderful 2020 年 4 月 12 日
Couple of things noted and suggestion added for the above code
Code point-1
tempa=sort(tempa);
Replace with
tempa=sortrows(tempa);
Code point-2
if tempa==a(j,:) %might NOT always work for Tables
Replace with
if isequal(tempa,a(j,:)) % better implementation

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by