random selection from 5 different vectors

2 ビュー (過去 30 日間)
Francesco Porretta
Francesco Porretta 2021 年 9 月 11 日
回答済み: Stephen23 2021 年 9 月 11 日
I have 5 vectors, from which I would like to extract 6 variables that should be i.i.d (Indipendent and Identically Distributed).
A = [1;2;3;2]; % 4x1
B =[4;5]; % 2x1
C = [7;8;9]; % 3x1
D = [0;1;2]; % 3x1
P = linspace(0,1,101); % 101x1
I want to extract (following the same uniform randomly distribution) 1 variable from A, from B, from C and from D, and 2 variables from P.
To use the same distribution and have a i.i.d set, I think that I need to use the same random function like 'rand' or 'randi' just one time on the overall vectors, but I do not know how to do it.
Someone can help??

採用された回答

Stephen23
Stephen23 2021 年 9 月 11 日
C = {[1;2;3;2],[4;5],[7;8;9],[0;1;2],linspace(0,1,101)};
V = {1,1,1,1,2};
F = @(v,n)v(randi(numel(v),1,n));
Z = cell2mat(cellfun(F,C,V,'uni',0))
Z = 1×6
2.0000 4.0000 9.0000 2.0000 0.2300 0.7700

その他の回答 (0 件)

カテゴリ

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