How do I limit the values in an array?

412 vues (au cours des 30 derniers jours)
Stuart Ellison
Stuart Ellison le 12 Juil 2017
Commenté : Walter Roberson le 30 Nov 2020
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!

Réponse acceptée

ES
ES le 12 Juil 2017
>> a = [0.203 0.506 0.167 0.904 1.671 5.247 0.037 0.679];
>> a(a>1)=1

Plus de réponses (1)

Jan Siegmund
Jan Siegmund le 27 Mai 2020
Modifié(e) : Jan Siegmund le 27 Mai 2020
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 commentaires
Tim Kuipers
Tim Kuipers le 30 Nov 2020
That doesn't work if A is an array. It gives an error
Walter Roberson
Walter Roberson le 30 Nov 2020
min(randn(3,3),1) works fine when I test it. Perhaps you accidentally defined min as a variable

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by