Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one vector is a subset of another?

Tags:

matlab

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?

like image 253
zebra Avatar asked Apr 11 '12 16:04

zebra


1 Answers

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));
like image 125
gnovice Avatar answered Oct 26 '22 21:10

gnovice