Random number selection from matrix column and from decimal number

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2021-9-11
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).')

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-9-11
编辑:Image Analyst 2021-9-11
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

更多回答(1 个)

David Hill
David Hill 2021-9-11
[A(randi(3,1,4)+[0 3 6 9]),rand(1,2)];

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by