Basic question regarding adding A to all numbers greater than B

For example, if I have an array like x = (1:10); How would I add 2 to each number in that array that is greater than 5?

回答 (1 件)

James Blackwell
James Blackwell 2018 年 2 月 1 日

0 投票

You could have an if function and go through the array element by element. If the number is greater than 5 you could add 2 to it :)
It sounds a bit like a homework question, so I don't think I'm supposed to give the code.

3 件のコメント

Angel
Angel 2018 年 2 月 1 日
Yes, it is a homework question. That is what I thought, but I'm not allowed to use if statements. Thanks, though.
Steven Lord
Steven Lord 2018 年 2 月 1 日
Since your homework assignment prohibits using if, its purpose is likely to familiarize you with logical indexing.
Angel
Angel 2018 年 2 月 1 日
I'll look into that, thank you!

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2018 年 2 月 1 日

コメント済み:

2018 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by