[Matlab] Determine if an array is subset of another

Problem

Given two arrays A and B, determine whether B is subset of A.

Solution

Combine ismember with all. Function ismember(B, A) will return an array with all elements set to true if, and only if, all elements in B are in A (which means B is a subset of A). Thus, all(ismember(B, A)) = true iff B is a subset of A.

References

[1] http://stackoverflow.com/questions/3918993/an-efficient-code-to-determine-if-a-set-is-a-subset-of-another-set
[2] http://www.mathworks.com/help/techdoc/ref/ismember.html
[3] http://www.mathworks.com/help/techdoc/ref/all.html

Advertisements

2 thoughts on “[Matlab] Determine if an array is subset of another”

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s