Sampling iteratively random value from a matrix/array

k = unique(ID_site)
rl = [0.7 0.8 0.85 0.95 1]
rs = [0.65 0.7 0.75 0.8 0.85 0.9 0.95]
im = [0.55 0.65 0.75 0.85 0.9 0.95 1]
ka = [0.92 1]
for i=1:numel(k)
tc1 = rl.*rs.*im.*ka
tc2 = rl.*rs.*im.*ka
tc3 = rl.*rs.*im.*ka
end
Hello,
I want to calculate the products tc1, tc2 and tc3 several times (10000 nsamples) for each ID_site, sampling each time a random value from rl, rs, im and ka.
The output should display 10000 results of tc1, tc2 and tc3 for each test site.
Does anyone knows how to do that?
Thank you very much

 採用された回答

Stephen23
Stephen23 2019 年 8 月 30 日

1 投票

No loops required, just use indexing:
k = 1:13; % fake data
n = numel(k);
rl = [0.7 0.8 0.85 0.95 1]
rs = [0.65 0.7 0.75 0.8 0.85 0.9 0.95]
im = [0.55 0.65 0.75 0.85 0.9 0.95 1]
ka = [0.92 1]
% random indices:
rl_x = randi(numel(rl),1,n);
rs_x = randi(numel(rs),1,n);
im_x = randi(numel(im),1,n);
ka_x = randi(numel(ka),1,n);
% output:
out = rl(rl_x) .* rs(rs_x) .* im(im_x) .* ka(ka_x)

3 件のコメント

Enrico D'Addario
Enrico D'Addario 2019 年 9 月 2 日
Thank you Stephen
but I need to calculate "out" for 1000 times for each "k".
I have e.g. 110 id_sites, for each id_site I need 1000 values of "out" .
I think the code should be like:
for each id_site, calculate 1000 values of out, sampling randonmly values from the variables rl, rs, im and ka
Am I wrong?
Stephen23
Stephen23 2019 年 9 月 2 日
編集済み: Stephen23 2019 年 9 月 2 日
"Am I wrong?"
So far nothing you have described requires a loop. You can trivially generate the random indices with size 1000x110 and then get all of your results at once.
s = 1000; % number of samples
k = 1:110; % fake data
n = numel(k);
rl = [0.7,0.8,0.85,0.95,1];
rs = [0.65,0.7,0.75,0.8,0.85,0.9,0.95];
im = [0.55,0.65,0.75,0.85,0.9,0.95,1];
ka = [0.92,1];
% random indices:
rl_x = randi(numel(rl),s,n);
rs_x = randi(numel(rs),s,n);
im_x = randi(numel(im),s,n);
ka_x = randi(numel(ka),s,n);
% output:
out = rl(rl_x) .* rs(rs_x) .* im(im_x) .* ka(ka_x);
Enrico D'Addario
Enrico D'Addario 2019 年 9 月 2 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by