1

I need to compare two different arrays in Matlab. It's going to be used for a Yahtzee game. If I have an array that contains [1 2 3 4] and an array that contains [1 2 3 4 5], how do I check if the first array is contained within the second array. I just need to know a T/F result, not anything about which elements are missing, etc.

2 Answers 2

4

ismember will do it. For example:

x = [1 2 3 4]
y = [1 2 3 4 5]
all(ismember(x,y))

You can also use setdiff. For example:

isempty(setdiff(x,y))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks... idk why you have to take this obscure route to check strict equality though. :-(
For strict equality, use isequal or isequalwithequalnans. This question was to check for set subset.
0

another option,

all(intersect(x,y)==x)

but ismember is probably more efficient....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.