How to remove Zeros in the array
조회 수: 13 (최근 30일)
이전 댓글 표시
r=5;
for x=1:r
for y=1:x
A(x,y)=x.*y;
end
end
In this code I want Matlab to print the multiplication table using a nested for loop. The for loop works but does not print what I want.
I want it to look something like this:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
How do I get Matlab to not place 0s in the empty spaces?
댓글 수: 0
답변 (1개)
Image Analyst
2020년 11월 6일
Try this:
r=5;
for row=1:r
for col=1:row
A(row,col)=row.*col;
end
fprintf('%4d ', A(row, 1:row))
fprintf('\n');
end
A
You'll see this:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
A =
1 0 0 0 0
2 4 0 0 0
3 6 9 0 0
4 8 12 16 0
5 10 15 20 25
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!