How to use conditional statement for this case?

for example, x=[1:1:9] and I want y=x*2 for x=1,2,3, y=x+2 for x=4,5,6 and y=x*3 for x=7,8,9 so the result is y=[2,4,6,6,7,8,21,24,27]. Can anyone help me to write the correct code for this case? Thank you

 採用された回答

Stephen23
Stephen23 2017 年 5 月 13 日

0 投票

>> x = 1:1:9;
>> y = [x(1:3)*2,x(4:6)+2,x(7:9)*3]
y =
2 4 6 6 7 8 21 24 27

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by