replace array value with if condition

Hello
hello, i have samply array,I would like to replace all numbers greater than 5 to be 5 and lower than 4 to be 2
in my experiment above its success if condition greater than 5. How about lower than 2 it is posible? , and the result i want A=[2 2 2 2 5 5 5 5 5]
A=[4 4 4 4 5 6 7 8 9 ]
max(A)
min(A)
if max(A)>=5
A(A>=5)=5
end
Thanks a lot

回答 (1 件)

KSSV
KSSV 2021 年 7 月 22 日

0 投票

Let A be your array.
A(A>=5) = 5 ;
A(A<4) = 2 ;

3 件のコメント

syaika bayu
syaika bayu 2021 年 7 月 22 日
its work.
so i dont need max(A) and min(A) to do it why ?
KSSV
KSSV 2021 年 7 月 22 日
編集済み: KSSV 2021 年 7 月 22 日
If you know the values, give the values, if values are not known, use max amd min.
syaika bayu
syaika bayu 2021 年 7 月 22 日
okey i understand
thank you very much, kssv

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

カテゴリ

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

質問済み:

2021 年 7 月 22 日

コメント済み:

2021 年 7 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by