Help with this problem please
3 次查看(过去 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 ?
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!