matrix ismember of another matrix

1 view (last 30 days)
alex
alex on 17 Mar 2014
Commented: alex on 17 Mar 2014
hello!
i have
A=[1 2 3 4 5]
B=[2 4]
i want to create D=[1 3 5], which is the A without the elements of B
j=1;
A=[1 2 3 4 5];
B=[2 4];
C=ismember(A,B);
for i=1:5
if C(i)==0
A(j)=A(i);
j=j+1;
end
end
for i=1:3
D(i)=A(i);
end
the code above works fine,but i want to know if there is any other way to do this,for any matrices with any dimentions
Thank you very much!

Accepted Answer

per isakson
per isakson on 17 Mar 2014
Edited: per isakson on 17 Mar 2014
A=[1 2 3 4 5];
B=[2 4];
C = setdiff( A, B )
returns
C =
1 3 5
Not sorted:
C = setdiff( A, B, 'stable' );
.
This works with vectors, however, "any matrices with any dimentions" is a different story
  2 Comments
alex
alex on 17 Mar 2014
thank you very much!
alex
alex on 17 Mar 2014
i have a problem here
A = 2:.1:15
B=[9.4000 10.4000]
C=setdiff(A,B)
the elements 9.4000 and 10.4000 are in the C
however,if B=[9.3000 10.3000] then the setdiff works fine.
any idea why this happens?

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!