How to multiple set of normally distributed data?

1 view (last 30 days)
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?

Accepted Answer

dpb
dpb on 22 Jul 2021
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.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!