How to choose a random number between 3 values in matrix?

8 visualizzazioni (ultimi 30 giorni)
Hi all!
i have a matrix (m*n), i want to fill it randomly with just 3 values, for example i want to get matrix with these values : 3,6,7
the manner to choose between those values is random.
it is possible to do it?

Risposta accettata

Walter Roberson
Walter Roberson il 6 Set 2022
If you have the statistics toolbox you could use randsample(). But otherwise:
choices = [3, 6, 7];
m = 4; n = 3; %for example
output = choices(randi(length(choices), m, n))
output = 4×3
7 6 7 7 6 3 3 7 7 3 6 7

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by