Help with this problem please

1 回表示 (過去 30 日間)
Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019 年 4 月 26 日
編集済み: Abdelmalek Benaimeur 2019 年 4 月 26 日
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 日
編集済み: Abdelmalek Benaimeur 2019 年 4 月 26 日
Yes this what i want how to detect if the matrix contains only 0 and 1 or not

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by