Help with looping and average

1 次查看(过去 30 天)
Hi, I was trying to calculate the average of 1000 randoms numbers and after that, to do a looping 2000 times and get the average of those samples.
Here is my code, I'm begginer, any help is good.
///////////////////////////////////////////////////////
amostra=1000;
alpha1 = randn(1,1000);
alpha2 = randn(1,1000);
for i=1:amostra
soma=size(amostra,1);
if(sqrt(alpha1)+sqrt(alpha2)<1)
x=1;
else
x=0;
end
soma(i)=soma+x
end
plot(soma); xlabel('Amostras');
/////////////////////////////////////////////////////
Thanks

采纳的回答

Image Analyst
Image Analyst 2012-4-1
The code doesn't seem to do that. There is no 2000. And what are those sqrt's in there for? Do you have to do looping? Why not just do
for run = 1 : 2000
% For each run, get a new set of random numbers.
data = randn(1, 1000);
% Get the mean of this set of random numbers.
meanOfData(run) = mean(data);
end
% Now get the mean of all the sets of numbers.
meanOfMeans = mean(meanOfData)

更多回答(2 个)

Rick Rosson
Rick Rosson 2012-4-1
Please format your code.

rodrigo Munhoz
rodrigo Munhoz 2012-4-1
Thanks for your help

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by