Borrar filtros
Borrar filtros

why is this not outputting the correct values

1 visualización (últimos 30 días)
Will
Will el 25 de Nov. de 2023
Comentada: Star Strider el 25 de Nov. de 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

Respuesta aceptada

Star Strider
Star Strider el 25 de Nov. de 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 comentarios
Will
Will el 25 de Nov. de 2023
this was exactly the case much appreciated
Star Strider
Star Strider el 25 de Nov. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by