for loop in for loop not storing all variables
1 回表示 (過去 30 日間)
古いコメントを表示
Dameon Solestro
2021 年 11 月 22 日
コメント済み: Walter Roberson
2021 年 11 月 24 日
In the simplest way, I am trying to get 3 random (x,y) data sets per group with a total of 5 groups. (So 15 x coordinates and 15 y coordinates in total). Every time I use my mean(x) to find the average x. My for loop only calculates the mean for my groups of x coordinates. How do i do it for all 15 x coordinates?
for j=1:5
for i=1:3
x(i)= rand();
y(i)= rand();
hold on
end
hold on
h=mean(x)
end
0 件のコメント
採用された回答
Walter Roberson
2021 年 11 月 23 日
for j=1:5
for i=1:3
x(i,j)= rand();
y(i,j)= rand();
hold on
end
hold on
h(j)=mean(x(:,j))
end
mean(x,1)
mean(x,2)
You can see that if you stored all of the values, that mean(x,1) calculates the same thing as taking the mean of each column at the time you generate the column.
0 件のコメント
その他の回答 (1 件)
Dameon Solestro
2021 年 11 月 24 日
1 件のコメント
Walter Roberson
2021 年 11 月 24 日
In the code I posted in https://www.mathworks.com/matlabcentral/answers/1593204-for-loop-in-for-loop-not-storing-all-variables#answer_837899 then you would just use
x - mean(h)
Note that each element of your h is a mean of a column, so mean(h) would be mean of the means
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!