A function that counts the number of zeros in a vector
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
D = ones(1,20);
for i = 1:20;
if mod(i,2) == 0;
D(i) = 0;
end
end
---------
D = [ 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]
and I need A function that counts the number of zeros in a vector I know there are 10 zeros but how to earn? I found only this but is not enough.
sum(find(D==0)/11) = 10
0 Kommentare
Antworten (3)
Dave B
am 12 Okt. 2021
you were pretty close, you just didn't need the find, or the /11
D = [ 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]
sum(D==0)
0 Kommentare
Jan
am 12 Okt. 2021
Beside
sum(D == 0)
nnz(D)
This would be working also, if the elements are 0 or 1 only:
numel(D) - sum(D)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Analysis 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!