Help with this problem please

4 vues (au cours des 30 derniers jours)
Abdelmalek Benaimeur
Abdelmalek Benaimeur le 26 Avr 2019
i want to write a script which tells me if the matrix is binary or not
the idea is to calculate the number of elements which are not equal to 0 or 1
if this number is superior to 0 that means the matrix contains at least a number superior to 1 so the matrix is not binary
if the number is equal to 0 that means the matrix contains only 0 and 1 so it's binary
so i did this
A=[0 1 2 4;1 0 3 5;0 0 2 6;4 7 8 9];
n=size(A,1);
m=size(A,2);
k=0;
for i=1:n
for j=1:m
if A(i,j)~= any([0;1])
k=k+1;
end
end
end
if k==0
dip('the matrix is binary');
else
disp('the matrix is real');
end
but i don't get the desired result any help please to make this script correct ?

Réponse acceptée

Walter Roberson
Walter Roberson le 26 Avr 2019
if ~any(A(i,j) == [0; 1])
Note that as soon as you find the first place, then you know the answer for the entire matrix, so you do not need to keep looking.
Note... there are much more efficient ways.
Question: do you need to find out whether the matrix is binary, containing only 0 and 1 values, or do you need to find out of the matrix is bi-level, containing at most two unique values? For example, some ways of representing "black and white" use 0 for black and 1 for white, but other ways of representing "black and white" use 0 for black and 255 for white.

Plus de réponses (1)

Abdelmalek Benaimeur
Abdelmalek Benaimeur le 26 Avr 2019
Modifié(e) : Abdelmalek Benaimeur le 26 Avr 2019
Yes this what i want how to detect if the matrix contains only 0 and 1 or not

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by