problems with the beginner examples

7 Ansichten (letzte 30 Tage)
linofex
linofex am 24 Feb. 2015
Beantwortet: linofex am 24 Feb. 2015
hi all, I have just started to read this guide Matlab prime R2014b, but I have a problem. At page 1-24 there an examples of for cicle that at next page shows the results. If i copy the same scripts, my matlab r2011b shows a different result. this is the script
nsamples=5;
npoints=50;
for k = 1:nsamples
iterstring=['iteration #', int2str(k)];
disp(iterstring);
currentdata=rand(npoints,1);
samplemean(k) = mean(currentdata)
end
overallmean = mean(samplemean)
In my case, at every iteration samplemean shows me 5 different randnumbers, and not 1(then2, then3..) as I aspect.
why?where is my mistake? sorry the stupid question, but if I don't understand at the beginning, I will not understand after.
EDIT after some trials I see that the result i think is true (but i think that mean(currentdata) should show only one valor) because, at every iteration the are 5 randnumber but for example at the second iteration, the first num is the first num of the first interaction and the second num is the same number showed at the third iteraction in the 2 position until the 5 iteration with the 5 meannum (the photo is shows better) But why in the guide the numbers are 'alone'.

Akzeptierte Antwort

John D'Errico
John D'Errico am 24 Feb. 2015
If you clear your workspace, so that samplemean is NOT defined when you start to run the loop, then it will look as you expected. However, IF you run that loop repeatedly, WITHOUT a clear in between runs, then samplemean is already defined as a vector of length 5, and it is full of numbers in advance.
One thing to take from this is that clearing your workspace can sometimes be a good thing. It is often a very bad thing too of course. So you are best off learning to use and write functions, which have their own workspaces. One thing at a time though.
(By the way, I'm quite happy to see that you are reading the tutorials and thinking about what you see, as well as trying out the examples.)

Weitere Antworten (1)

linofex
linofex am 24 Feb. 2015
Solved!! Infact I have written some scripts and at the first times the results was correct!! Thank you very much for the very speedy answer!! Regards, Alessandro

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by