フィルターのクリア

Simple for/cond statement

2 ビュー (過去 30 日間)
Salar
Salar 2016 年 2 月 25 日
回答済み: Geoff Hayes 2016 年 2 月 25 日
Hello,
This can be considered MATLAB 081 level! I'm just looking for a for statement that does this for me:
I have a column vector that has positive and negative elements, and I want to add an integer, let's say 50 to every single of these elements that are negative.
I would really appreciate anybody taking the time.
Sincerely,

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 2 月 25 日
mask = YourVector<0;
YourVector(mask) = YourVector(mask) + 50;
Or if you really want to get down to one statement:
YourVector = YourVector + 50 * (YourVector < 0);

Geoff Hayes
Geoff Hayes 2016 年 2 月 25 日
Salar - since your homework requires that you use a for loop, then take a look at its documentation. You will want to iterate over each element of your array (see length to get the number of elements in your array). At each iteration of the loop, you will use if to determine if the element is positive or negative. If the latter, then add 50.

カテゴリ

Help Center および 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