matrix ismember of another matrix

3 次查看(过去 30 天)
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!

采纳的回答

per isakson
per isakson 2014-3-17
编辑:per isakson 2014-3-17
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 个评论
alex
alex 2014-3-17
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by