How do I limit the values in an array?

This should be an easy one.
If I have an array with...
0.203 0.506 0.167 0.904 1.671 5.247 0.037 0.679
How do I amend the individual values within the array that are greater than 1 and set them to 1?
e.g.
0.203 0.506 0.167 0.904 1.000 1.000 0.037 0.679
Thanks!

 採用された回答

ES
ES 2017 年 7 月 12 日

3 投票

>> a = [0.203 0.506 0.167 0.904 1.671 5.247 0.037 0.679];
>> a(a>1)=1

その他の回答 (1 件)

Jan Siegmund
Jan Siegmund 2020 年 5 月 27 日
編集済み: Jan Siegmund 2020 年 5 月 27 日

2 投票

There is also a matlab function doing this:
A = min(A,1);
It takes A if A is the minimum of the two and 1 if 1 is the minimum of the two.

2 件のコメント

Tim Kuipers
Tim Kuipers 2020 年 11 月 30 日
That doesn't work if A is an array. It gives an error
Walter Roberson
Walter Roberson 2020 年 11 月 30 日
min(randn(3,3),1) works fine when I test it. Perhaps you accidentally defined min as a variable

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

カテゴリ

製品

質問済み:

2017 年 7 月 12 日

コメント済み:

2020 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by