I wrote this code in which all the elements of a 2D matrix below or equal to 90 are supposed to be zero after the loop gets evaluated. Ain't working.
조회 수: 1 (최근 30일)
이전 댓글 표시
So,I wrote this code in which all the elements of a 2D matrix below or equal to 90 are supposed to be zero after the loop gets evaluated.It does sets some elements below or equal to 90 to zero. But it always leaves out some elements,not converting them into zero.Where have I committed the mistake in this code?
Here is the code-
for i=1:size(a,1)
for j=1:size(a,2)
if a(i,j)<=90
a(i,j)=0;
else
break;
end
end
end
If I give this as input-
a =
1 2 90
91 95 78
65 98 99
It produces the following output-
a =
0 0 0
91 95 78
0 98 99
See it didn't convert 78 to zero.
댓글 수: 0
채택된 답변
Walter Roberson
2016년 9월 22일
Your code is written so that for any one row, as soon as it finds an element that is more than 91, it should stop scanning the row. You should read more about what "break" means.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!