problems with the beginner examples
조회 수: 4 (최근 30일)
이전 댓글 표시
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'.
댓글 수: 0
채택된 답변
John D'Errico
2015년 2월 24일
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.)
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 GPU Computing in MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!