# Making a matrix of R*C size that takes the values randomly from an array- Y.

4 vues (au cours des 30 derniers jours)
MAzharul Islam le 18 Août 2021
Commenté : MAzharul Islam le 18 Août 2021
I want to make a random matrix of R*C size that takes the values randomly from an array Y.
For example I want to make a 7*8 matrix that takes value from this array Y = [ 1 6 1 8 4 5], and place them randomly in the matrix.. I have tried randi command to do this, but randi doesn't take fixed values like I have stated above.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Wan Ji le 18 Août 2021
Hi, friend! randi is OK, it can help, look
Y = [ 1 6 1 8 4 5];
R = 7; C = 8;
indices = randi([1,numel(Y)], R, C);
A = Y(indices)
The result is
A =
8 1 5 4 4 4 1 1
1 1 8 1 1 5 6 1
5 1 1 1 6 1 1 1
8 1 8 5 1 1 4 5
5 1 8 8 1 1 8 8
5 8 1 8 8 6 6 1
1 1 5 1 4 4 8 4
Wish you will like it
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Wan Ji le 18 Août 2021
Here, because 1 appears in Y array twice, its appearance possibility in matrix A is twice as much as other numbers( 6,8,4 and 5).

Connectez-vous pour commenter.

### Plus de réponses (1)

Yazan le 18 Août 2021
i1 = 7; i2 = 8;
y = [1 6 1 8 4 5];
randIdx = randi(length(y), 1, i1*i2);
data = reshape(y(randIdx), i1, i2)
data = 7×8
6 4 5 6 5 1 1 4 1 4 1 1 1 1 1 1 1 1 1 1 8 1 6 1 6 4 5 6 1 6 6 8 4 5 8 1 6 4 8 8 1 8 4 8 4 5 8 1 6 6 8 1 4 6 5 1
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
MAzharul Islam le 18 Août 2021
Thanks. I wish I could accept both of Your answers, but..... I can't. Matlab doesn't allow it

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

R2012a

### Community Treasure Hunt

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

Start Hunting!

Translated by