Replace negative values in a matrix with a modified version of themselves.

28 ビュー (過去 30 日間)
Jacob Babcock
Jacob Babcock 2023 年 3 月 22 日
編集済み: Manikanta Aditya 2023 年 3 月 22 日
I am writing a code where I have an array of a mix of positive and negative numbers. If the value of a number is negative I want to add 18 to that number.
for example A = [1 3 5 7 9 -2 -4 -1 6 5 4 3 ]
then magic
then A = [1 3 5 7 9 16 14 17 6 5 4 3]
thank you!

回答 (1 件)

Manikanta Aditya
Manikanta Aditya 2023 年 3 月 22 日
編集済み: Manikanta Aditya 2023 年 3 月 22 日
Hi Jacob,
As per my understanding, you want to add '18' to a negative number in the mix of positive and negative numbers.
Here is how you can do it:
A = [1 3 5 7 9 -2 -4 -1 6 5 4 3]
A(A<0)=A(A<0)+18 % add 18 to negative numbers
disp(A); % display the updated array
I hope this resolves your issue.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by