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