Cube Every Third Integer Using Nested Loop Structure

Attempting to cube every third integer in an array using a for and if loop structure. Note, this is my first time creating a nested loop structure and need simple straightforward explanation as to why the code does not work.
for i = [1:16]
if (i/3==0)
i^3
else
i
end
end

回答 (1 件)

VBBV
VBBV 2022 年 2 月 8 日

1 投票

for i = [1:16]
if (mod(i,3)==0)
i^3
else
i
end
end
i = 1
i = 2
ans = 27
i = 4
i = 5
ans = 216
i = 7
i = 8
ans = 729
i = 10
i = 11
ans = 1728
i = 13
i = 14
ans = 3375
i = 16

2 件のコメント

faith igo
faith igo 2022 年 2 月 8 日
Thank you!
VBBV
VBBV 2022 年 3 月 30 日
if this solved your problem, please accept the answer :) thanks

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 2 月 8 日

コメント済み:

2022 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by