check if elements in an array are equal to 0 1 or 2

i have been given a random sqaure matrix. how do i check that all the elements witihin my array are equal to 0,1 or 2?

 Accepted Answer

Stephen23
Stephen23 on 21 Nov 2018
Edited: Stephen23 on 21 Nov 2018
"how do i check that all the elements witihin my array are equal to 0,1 or 2? "
>> A = randi([0,2],4) % should pass
A =
1 0 0 2
1 2 0 2
2 0 2 2
1 1 1 1
>> all(ismember(A(:),0:2))
ans = 1
>> A = randi([0,9],4) % should fail
A =
8 0 3 0
6 6 3 6
6 0 5 1
4 2 1 1
>> all(ismember(A(:),0:2))
ans = 0

More Answers (1)

a=randi([0 2],2)
unique(a(:)) %returns unique numbers in a matrix

Tags

Asked:

on 21 Nov 2018

Edited:

on 21 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!