How do you automatically put values into an array when in a loop?

3 次查看(过去 30 天)
I currently have the for loop below. where NoS is the number of subjects and NoJ is the number of jumps.
NoS = 6
NoJ = 4
bars = NaN(NoS*NoJ,1);
errors = NaN(NoS*NoJ,1);
for i=1:NoS*NoJ
clear ave stdev
ave = mean(data((i*NoJ)-3:(i*NoJ),3));
stdev = std(data((i*NoJ)-3:(i*NoJ),3));
bars(i) = ave;
errors(i) = stdev;
end
At the moment this imports the means and standard deviations straight into the arrays bars and errors in one long column (a vector). I however want it so it will each subject data into a new row in the array. In this example that would mean the array would be a 6x4 as there are 6 subjects and 4 jump types for each subject. I need the first 4 values to go in spaces (1,1) (1,2) (1,3) and (1,4) and then for it to move down to the next row and import the next 4 values in the same fashion.
  1 个评论
Dave
Dave 2013-4-30
I solved my problem by redefining my NaN matrices as a 4x6. I then transversed the matrices after the loop to give me the 6x4 matrix I was looking for.
bars = NaN(4,NoS);
errors = NaN(4,NoS);
for i=1:NoS*NoJ
clear ave stdev
ave = mean(data((i*NoJ)-3:(i*NoJ),3));
stdev = std(data((i*NoJ)-3:(i*NoJ),3));
bars(i) = ave;
errors(i) = stdev;
end
bars1 = bars'
stdev1 = stdev'
If anyone knows any more efficient ways of doing this then please let me know!

请先登录,再进行评论。

回答(1 个)

Iman Ansari
Iman Ansari 2013-4-30
Hi. Without loop:
NoS = 6;
NoJ = 4;
data1=reshape(data(1:96,3),4,24);
bars=mean(data1);
errors=std(data1);
bars=reshape(bars,4,6)';
errors=reshape(errors,4,6)';

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by