How to replace certain numbers in an array

Could anybody help me solve the problem please? I have a sample array a=[6 8 7 4 12 10 16 11], I would like to replace all numbers greater than 10(>10) to be 20-a, the expected result should be a=[6 8 7 4 8 10 4 9]. Thank you very much.

 採用された回答

Image Analyst
Image Analyst 2018 年 1 月 2 日

1 投票

Try this:
a=[6 8 7 4 12 10 16 11]
% Find indexes more than 10:
indexesToReplace = a > 10;
% Replace the index values with 20 minus the original number (20-a)?
a(indexesToReplace) = 20 - a(indexesToReplace)

その他の回答 (1 件)

KSSV
KSSV 2018 年 1 月 2 日
編集済み: KSSV 2018 年 1 月 2 日

1 投票

a=[6 8 7 4 12 10 16 11] ;
a(a>10) = 20-a(a>10) ;

2 件のコメント

MS SIM
MS SIM 2018 年 1 月 2 日
Thanks for the answer. However, this replaces the number greater than 10 with 20. How about replacing the number with 20 minus the original number (20-a)?
KSSV
KSSV 2018 年 1 月 2 日
編集済み: KSSV 2018 年 1 月 2 日
okay....a was the original number.....edited the answer.

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

カテゴリ

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

質問済み:

2018 年 1 月 2 日

編集済み:

2018 年 1 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by