Getting rid of zeros in matrix

2 views (last 30 days)
Katrina
Katrina on 14 Sep 2015
Commented: Katrina on 17 Sep 2015
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! :)

Accepted Answer

Jan
Jan on 14 Sep 2015
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!