フィルターのクリア

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 Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by