Help with this problem please

1 次查看(过去 30 天)
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 ?

采纳的回答

Walter Roberson
Walter Roberson 2019-4-26
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.

更多回答(1 个)

Abdelmalek Benaimeur
编辑:Abdelmalek Benaimeur 2019-4-26
Yes this what i want how to detect if the matrix contains only 0 and 1 or not

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by