Clear Filters
Clear Filters

Random number selection from matrix column and from decimal number

3 views (last 30 days)
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?
  1 Comment
Walter Roberson
Walter Roberson on 11 Sep 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).')

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 11 Sep 2021
Edited: Image Analyst on 11 Sep 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);
v % Show in command window

More Answers (1)

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


Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!