making a matrice with conditions

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

 Accepted Answer

Ameer Hamza
Ameer Hamza on 14 Nov 2020
Edited: Ameer Hamza on 14 Nov 2020
You can use min()
x = randi([0 19], 1, 20);
y = min(x+2, 20);

3 Comments

why do you use min()? I thought you would use max() because it can be max 20.
You use min, because you want MATLAB to take the SMALLER of x+2 and 20. Ergo, min.

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!