why is this not outputting the correct values

1 view (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 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
.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!