Seldom
Seldom

Reputation: 269

Comparing different sized arrays in Matlab

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.

Upvotes: 1

Views: 1563

Answers (2)

user2041376
user2041376

Reputation:

another option,

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

but ismember is probably more efficient....

Upvotes: 0

Pursuit
Pursuit

Reputation: 12345

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))

Upvotes: 4

Related Questions