Filter löschen
Filter löschen

Getting rid of zeros in matrix

1 Ansicht (letzte 30 Tage)
Katrina
Katrina am 14 Sep. 2015
Kommentiert: Katrina am 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! :)

Akzeptierte Antwort

Jan
Jan am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by