Random number selection from matrix column and from decimal number

1 回表示 (過去 30 日間)
Francesco Porretta
Francesco Porretta 2021 年 9 月 11 日
編集済み: Image Analyst 2021 年 9 月 11 日
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 Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by