I am new to Matlab, and am trying to understand how for loops would work. Specifically, I wish to generate 100 draws from a standard uniform distribution and calculate the mean each time, and repeat this procedure 500 times. Thereafter, I wish to store the mean in a vector.
One way to achieve this is with:
U = [];
Average = [];
for i = 1:500
U = rand(1, 100);
Average = [Average mean(U)];
U = [];
end
The intuition is straightforward. I create an empty vector for U and the average. Thereafter, I draw 100 realizations from the standard uniform, calculate the average, store the average, empty the U vector and repeat. The procedure works, but I just wish to clarify one thing: although this is a for loop, with i being the loop variable, i does not show up in the body anywhere. My question is: if the loop variable does not appear in the body, is the procedure simply repeated the number of times equal to the number of 1 unit increments as specified in the for command?
Iwill be assigned a different value every loop iteration, whether you use it or not.U, those two lines of code are superfluous. Also, you can do this without a loop:mean(rand(500,100),2)