matrix ismember of another matrix

1 Ansicht (letzte 30 Tage)
alex
alex am 17 Mär. 2014
Kommentiert: alex am 17 Mär. 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!

Akzeptierte Antwort

per isakson
per isakson am 17 Mär. 2014
Bearbeitet: per isakson am 17 Mär. 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 Kommentare
alex
alex am 17 Mär. 2014
thank you very much!
alex
alex am 17 Mär. 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by