How to generate a matrix of random integers from 55 to 100?

179 vues (au cours des 30 derniers jours)
Yuval
Yuval le 27 Mar 2013
Commenté : Carlos Flores le 25 Sep 2022
Using function rand, should a matrix of random integers in the interval [55..100] be generated thus:
M = ceil((rand(5,5)+(11/9))*45);?

Réponse acceptée

the cyclist
the cyclist le 27 Mar 2013
Modifié(e) : the cyclist le 27 Mar 2013
That looks right if you have to use rand(), for example if this is a school assignment.
Easier would be
M = randi([55 100],5,5);
  2 commentaires
Joel Rodriguez
Joel Rodriguez le 30 Mar 2020
great
Carlos Flores
Carlos Flores le 25 Sep 2022
Thank you it was very helpful!

Connectez-vous pour commenter.

Plus de réponses (2)

Pranali Navale
Pranali Navale le 14 Avr 2021
m=rand([1 20],5,5)
  2 commentaires
Steven Lord
Steven Lord le 14 Avr 2021
No, that will not work. The randi function accepts as its first input a range to control how large or small the generated random integer values can be. The rand function does not.
m = rand([1 20], 5, 5);
Error using rand
Size inputs must be scalar.
See the examples in the documentation for rand if you want to generate random values (not necessarily integer values) between 1 and 20.
Nikunj Aswani
Nikunj Aswani le 5 Fév 2022
use randi instead of rand

Connectez-vous pour commenter.


Luis Freitas
Luis Freitas le 22 Fév 2022
ceil((rand(5,5)+(11/9))*45)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by