1 == 0 ?
이전 댓글 표시
function nbDeZero = EstMatriceCreuse(matrice)
matrice = [1,2,3,0,0,0,0,0;1,2,3,4,5,0,0,0];
validateattributes(matrice,{'double'},{'2d','nonempty'})
[m,n] = size(matrice);
o = 0 ;
for i = 1 : m
for j = 1 : n
if matrice(m,n) == 0
o = o + 1;
end
end
end
fprintf('%d',o);
end
For some reason my variable o increments everytime...even if the number is not zero. I'm kinda of a newbie so I don't know what is the problem. please help.
채택된 답변
추가 답변 (1개)
Image Analyst
2022년 3월 9일
Get rid of this line
matrice = [1,2,3,0,0,0,0,0;1,2,3,4,5,0,0,0];
because it just blows away any matrix you pass in via the argument list.
Also you're using the wrong indexes in
if matrice(m,n) == 0
That just looks at the very last element in the lower right corner of the matrix.
It should be
if matrice(j, i) == 0
Plus you're never assigning nbDeZero. You need to do
nbDeZero = o;
at the end of the function. Also don't use o as the variable name because it looks too much like 0. Use numNonZeros or some other descriptive variable name.
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!