Getting rid of zeros in matrix

2 次查看(过去 30 天)
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