Generating a normal distrbution random numbers and setting them to catagories
1 view (last 30 days)
Show older comments
Hello,
Im very much new to matlab and would love help on this script.
I have to generate (n) amount of numbers, then set a,b and c to their catagories a is <-0.25 , b is >-0.25 and <=0.25 , c is >0.25
as well as find the sum of each catagories
function [a,b,c,Asum,Bsum,Csum]=random_gen(n)
a=0;
b=0;
c=0;
Asum=0;
Bsum=0;
Csum=0;
r=randn(1,n)
for n=1:n
if r(n)<-0.25
a=a+1
Asum=Asum+r(n)
elseif r(n)>=-0.25 && r(n)<=0.25
b=b+1
Bsum=Bsum+r(n)
elseif r(n)>0.25
c=c+1
Csum=sum+r(n)
end
end
3 Comments
Answers (2)
dpb
on 17 Jun 2021
function [abc,ABC]=random_gen(n)
r=randn(n,1); % generate column vector N rn
ix=discretize(r,[-inf -0.25 0.25 inf]); % bin into three bins
abc=arrayfun(@(i)x(ix==i),1:3,'UniformOutput',0); % return binned values (3-cell array)
ABC=accumarray(ix,r); % return bin sums (3-array)
end
0 Comments
Walter Roberson
on 17 Jun 2021
Asum = Asum+a(n)
You have
a(n)=r(n);
Suppose it is true for n = 2 and 5. Then assigning to a(2) would create a(1) as 0, and assigning to a(5) would create a 3 and a 4 as 0. And if the total run was 10 then a6 onwards would not exist.
0 Comments
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!