How to multiple set of normally distributed data?
조회 수: 1 (최근 30일)
이전 댓글 표시
I developed the following code to find the multiplication of 2 sets of random variables:
rng(0, 'twister');
b2=1.00; % average
a2=0.50; % standard deviation
x2=a2.*randn(10000000,1)+b2;
rng(0, 'twister');
b1=0.23; %average
a1=0.12; %standard deviation
x1=a1.*randn(10000000,1)+b1;
x3=x2.*x1;
m1=mean(x3)
s1=std(x3)
c1=s1/m1;
Now I want to modify code by using "for" loop(target: to find x3=x2.*x1 for arrays of a1,a2,b1, b2)
rng(0, 'twister');
b2=[1.00];
a2=[0.50];
b1=[0.23];
a1=[0.12];
for i=1:length(b2)
x2=a2(i).*randn(10000000,1)+b2(i);
x1=a1(i).*randn(10000000,1)+b1(i);
x3(:,i)=x2.*x1;
m1(i)=mean(x3(:,i))
s1(i)=std(x3(:,i))
c1(i)=s1(i)/m1(i);
end
However, the resluts of "for" loop is not coincede with the results of previous method.
How can I solve this problem?
댓글 수: 0
채택된 답변
dpb
2021년 7월 22일
You rest the RNG before each of the two sets in first code segment but only at the beginning of the loop in the second so the two x1 sequences aren't the same.
No idea what the purpose of this might be for, but either
rng(0, 'twister');
b2=1.00; % average
a2=0.50; % standard deviation
x2=a2.*randn(10000000,1)+b2;
%rng(0, 'twister');
b1=0.23; %average
a1=0.12; %standard deviation
x1=a1.*randn(10000000,1)+b1;
x3=x2.*x1;
m1=mean(x3)
s1=std(x3)
c1=s1/m1;
without changing the second code segment or
b2=[1.00];
a2=[0.50];
b1=[0.23];
a1=[0.12];
for i=1:length(b2)
rng(0, 'twister');
x2=a2(i).*randn(10000000,1)+b2(i);
rng(0, 'twister');
x1=a1(i).*randn(10000000,1)+b1(i);
x3(:,i)=x2.*x1;
m1(i)=mean(x3(:,i))
s1(i)=std(x3(:,i))
c1(i)=s1(i)/m1(i);
end
leaving the first unchanged will produce the same overall sequence of randn()
It makes no sense to me to do either in isolation here; I suppose there might be some situation this mimics that it would be a reason for one or the other cases this simulates.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!