필터 지우기
필터 지우기

why is this not outputting the correct values

조회 수: 1 (최근 30일)
Will
Will 2023년 11월 25일
댓글: Star Strider 2023년 11월 25일
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

채택된 답변

Star Strider
Star Strider 2023년 11월 25일
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
Will
Will 2023년 11월 25일
this was exactly the case much appreciated
Star Strider
Star Strider 2023년 11월 25일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by