0
rand(3,4)

generates the following 3x4 matrix for me:

0.4229    0.4709    0.6385    0.3196
0.0942    0.6959    0.0336    0.5309
0.5985    0.6999    0.0688    0.6544

How can I limit the values generated to a specific range? Like restricting them to values between 1 and 100. And how can I specify whether I want the random numbers to be float or int?

1

1 Answer 1

6

You can multiply by the range you want, and add the minimum value. So, to get a matrix with values in the range [1, 100], do:

1 + 99*rand(3,4)

If you want single or something else, use the typename argument to specify this.

If you want integers, use randi:

1 + randi(99,3,4)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.