Help with this problem please
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Abdelmalek Benaimeur
am 26 Apr. 2019
Bearbeitet: Abdelmalek Benaimeur
am 26 Apr. 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 ?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Apr. 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.
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!