Find element in matrix
Show older comments
Hello, I have a matrice and i whant to find if there are 2 iqual elements in diferent rows...
A =
1 2
3 5
2 4
6 1
In this case i should find the number 1 and 2. But i whant to find one at a time. Can someone help me with this? Thank´s.
1 Comment
Image Analyst
on 6 Apr 2012
A is comprised on ONLY integers, right? Else you need to consider the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Accepted Answer
More Answers (3)
Thomas
on 3 Apr 2012
A = [1 2
3 5
2 4
6 1 ];
intersect(A(:,1),A(:,2))
1 Comment
Sean de Wolski
on 3 Apr 2012
or ismember()
Thomas
on 6 Apr 2012
Try this
clear all
A = [1 2
3 5
2 4
6 1 ];
c=intersect(A(:,1),A(:,2));
for i=1:length(c)
[p(:,:),q(:,:)]=find(A==c(i));
l=A(p(:),:);
m=unique(l)';
z(i,:)=m(m~=c(i));
end
c % common elements 1 and 2
z % elements associated with 1=2,6 and 2=1,4
Output will be
c =
1.00
2.00
z =
2.00 6.00
1.00 4.00
Eduardo
on 6 Apr 2012
0 votes
Categories
Find more on Logical 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!