Reputation: 6531
I'm looking for a matlab way of doing this. Looping through seems easy enough. I have two vectors, say a = [1 2 3]
and b = [1 54 2 4 6 3]
. I want to determine if a
is a subset of b
. How is this done?
Upvotes: 9
Views: 12510
Reputation: 125
You could also do intersect of both sets and see if it is empty or not. C = intersect(A,B) returns the data common to both A and B with no repetitions.
Upvotes: 0
Reputation: 125854
Probably the easiest and quickest way to do this is to use the functions ISMEMBER and ALL:
isSubset = all(ismember(a, b));
You can also use SETDIFF and ISEMPTY, but this appears to be less efficient (it runs a little slower than the above in R2010b):
isSubset = isempty(setdiff(a, b));
Upvotes: 10