Effacer les filtres
Effacer les filtres

Getting rid of zeros in matrix

2 vues (au cours des 30 derniers jours)
Katrina
Katrina le 14 Sep 2015
Commenté : Katrina le 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! :)

Réponse acceptée

Jan
Jan le 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
  1 commentaire
Katrina
Katrina le 17 Sep 2015
Thank you!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by