필터 지우기
필터 지우기

Getting rid of zeros in matrix

조회 수: 2 (최근 30일)
Katrina
Katrina 2015년 9월 14일
댓글: Katrina 2015년 9월 17일
I need to find solutions to right triangles when I run my code I get the answers but it is separated by rows of all zeros like this:
0 0 0
0 0 0
0 0 0
0 0 0
3 4 5
0 0 0
0 0 0
0 0 0
0 0 0
6 8 10
0 0 0
0 0 0
5 12 13
and so on. Here is my code:
for a=1:50;
for b=a:50;
c=sqrt(a^2+b^2);
if c<=50 && c==floor(c)
matrix=[a,b,c];
mat(c,:)=matrix
end
end
end
mat
Any suggestions on how to fix this? Thank you! :)

채택된 답변

Jan
Jan 2015년 9월 14일
index = 0;
for a=1:50;
for b=a:50;
c = sqrt(a^2+b^2);
if c<=50 && c==floor(c)
index = index + 1;
mat(index,:)=[a,b,c];
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by