about random number and matrix

1 回表示 (過去 30 日間)
Zichao Wang
Zichao Wang 2020 年 5 月 25 日
コメント済み: Walter Roberson 2020 年 5 月 26 日
If I have only one random number rand(1) , R1 (1X N) and R2(1XN) are both vector of size N.
R1=a*[0:n-1];
R2=b*[0:n-1];
R2=rand(1)+R2; % add rand(1) to every element in R2
del=R1-R2' ; % "row vector-column vector" is interpreted as "matrix-matrix" with appropriate duplication of rows and columns
But i want to do monte carlo simulation so I need a lot of rand(1). For example rand(1000000,1). But i dont want to use any loops since it is unefficient.
How can I still do the two steps above ?
  5 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 25 日
That did not answer my question.
Your R1 is length n, and your R2 is length n as well, and you have 1000000 random numbers. What size of output of del do you want? n x n x 1000000 ? n x n x 1000000 x 1000000 ?
Zichao Wang
Zichao Wang 2020 年 5 月 25 日
I am not sure . I want each random can generate a corrponding del. So i need 100000 of del (n X n)
so n x n x 1000000 i guess

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 5 月 25 日
編集済み: Walter Roberson 2020 年 5 月 25 日
R1=a*[0:n-1];
R2=b*[0:n-1];
R2=rand(1, 1, 100000)+R2.'; % add rand to every element in R2
del=R1-R2 ; % "row vector-column vector" is interpreted as "matrix-matrix" with appropriate duplication of rows and columns
  6 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 26 日
Please re-check.
>> a = rand; b = rand; n = 5; R1=a*[0:n-1]; R2=b*[0:n-1]; R2=rand(1, 1, 100000)+R2.'; del=R1-R2 ;
>> size(del)
ans =
5 5 100000
Walter Roberson
Walter Roberson 2020 年 5 月 26 日
NB = 20; %number of bins
mind = min(del(:)); maxd = max(del(:));
bins = linspace(mind,maxd,NB); centers = mean([bins(1:end-1);bins(2:end)]);
counts = arrayfun(@(R,C) histcounts(del(R,C,:), bins), ndgrid(1:n,1:n), meshgrid(1:n,1:n), 'uniform', 0);
h = arrayfun(@(idx) bar(subplot(n,n,idx), centers, counts{idx}), 1:n^2);

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by