making a matrice with conditions

5 Ansichten (letzte 30 Tage)
Jens Petit-jean
Jens Petit-jean am 14 Nov. 2020
Kommentiert: Jens Petit-jean am 14 Nov. 2020
Hello,
I have a column variable with 20 random numbers between 0 and 19. How do I add +2 to all numbers but the numbers can be maximum 20?
Thanks in advance

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 14 Nov. 2020
Bearbeitet: Ameer Hamza am 14 Nov. 2020
You can use min()
x = randi([0 19], 1, 20);
y = min(x+2, 20);
  3 Kommentare
John D'Errico
John D'Errico am 14 Nov. 2020
You use min, because you want MATLAB to take the SMALLER of x+2 and 20. Ergo, min.
Jens Petit-jean
Jens Petit-jean am 14 Nov. 2020
ah thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by