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

### Dave (view profile)

on 30 Apr 2013

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.

Dave

### Dave (view profile)

on 30 Apr 2013

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!

## Products

No products are associated with this question.

### Iman Ansari (view profile)

on 30 Apr 2013

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)';
```