Random number selection from matrix column and from decimal number
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Francesco Porretta
el 11 de Sept. de 2021
Editada: Image Analyst
el 11 de Sept. de 2021
Hi all, I'm searching for a way to select random variables from each column of a matrix, together with two different random parameters between 0 and 1 (so, not integer...), without losing the i.i.d. condition (Indipendent and Identically Distributed).
For example:
A = [1 2 3 4;
5 6 7 8 ;
9 10 11 12]; % example of my 3x4 matrix
What I want to do is to obtain a vector v 1x6 with in the first 4 column a random variable taken from each column of A, and in the last 2 column of v 2 values between 0 and 1.
The only way that I found to do it is to use 2 times the function randi: one for the random selection of the four values from A, and one for the random selection of the 2 values between 0 and 1. However, using 2 times the function randi, the elements in v will be not i.i.d.
There is a way to use just one time the function for the overall random selection?
Thanks
1 comentario
Walter Roberson
el 11 de Sept. de 2021
The columns might be iid with respect to the other columns, but it does not follow that within one column that the rows are iid to each other. There could be a covariance matrix, or it could be something along the lines of exp(rand() .* (1:3).')
Respuesta aceptada
Image Analyst
el 11 de Sept. de 2021
Editada: Image Analyst
el 11 de Sept. de 2021
Did you try s aimple and intuitive for loop:
A = [1 2 3 4;
5 6 7 8 ;
9 10 11 12]; % example of my 3x4 matrix
% Get sizes of the array
[rowsA, columnsA] = size(A)
% Get random row indices for each column
aRowIndexes = ceil(rowsA * rand(1, columnsA))
% Initialize v with random numbers.
v = rand(1, (columnsA + 2));
% Assign the values from the random locations.
for col = 1 : columnsA
v(col) = A(aRowIndexes(col), col);
end
v % Show in command window
0 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!