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

조회 수: 1 (최근 30일)
Dave
Dave 2013년 4월 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