Effacer les filtres
Effacer les filtres

why is this not outputting the correct values

1 vue (au cours des 30 derniers jours)
Will
Will le 25 Nov 2023
Commenté : Star Strider le 25 Nov 2023
triangle:1
Side A: 3
Side B: 3
Hypotenuse: 6
Side A: 3
Side B: 6
Hypotenuse: 4
triangle:2
Side A:
Side B: 6
Hypotenuse: 6
Side A: 4
Side B: >>
this is the output when I run this script where as I expected to get only two outputs of a's and such, also where could the code have gotten so many values of 3.
pythtrips=[3 4 5;6 8 10]
for i=1:size(pythtrips,1)
e=pythtrips(i:1);
f=pythtrips(i:2);
g=pythtrips(i:3);
fprintf('triangle:%d\n',i)
fprintf('Side A: %d \nSide B: %d \nHypotenuse: %d \n' ,e,f,g)
end

Réponse acceptée

Star Strider
Star Strider le 25 Nov 2023
There is a syntax error, in that you used a colon operator ‘:’ instead of a comma in the ‘pythtrips’ references. The syntax is ‘legal’ in the sense that the colon operator creates a vector (so it would not have thrown an error), however its behaviour was not what you intended.
Try this —
pythtrips=[3 4 5;6 8 10]
pythtrips = 2×3
3 4 5 6 8 10
for i=1:size(pythtrips,1)
e=pythtrips(i,1);
f=pythtrips(i,2);
g=pythtrips(i,3);
fprintf('triangle:%d\n',i)
fprintf('Side A: %d \nSide B: %d \nHypotenuse: %d \n' ,e,f,g)
end
triangle:1
Side A: 3 Side B: 4 Hypotenuse: 5
triangle:2
Side A: 6 Side B: 8 Hypotenuse: 10
.
  2 commentaires
Will
Will le 25 Nov 2023
this was exactly the case much appreciated
Star Strider
Star Strider le 25 Nov 2023
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by