MATLAB Answers

## storing output of for loop

Asked by Andrea Bae

### Andrea Bae (view profile)

on 26 Jul 2019
Latest activity Commented on by Andrea Bae

### Andrea Bae (view profile)

on 26 Jul 2019
Accepted Answer by David K.

### David K. (view profile)

Sorry for the redundant question in advance, but I'm really stuck as a beginner.
Trying to reorganize my data RLF1 in 1000s as different columns of a matrix. My current for loop is supposed to break the data in sequential 1000 chunks for a total of 300 columns, but I'm having trouble storing the output into my matrix. Only saves the last output (RLF1(200001:300000)) over and over again.
i = 1:1000:length(RLF1(1:300000));
store = zeros(1000,length(i));
for n = 1 : 300;
for i = 1:1000:length(RLF1(1:300000));
store(:,n) = RLF1(i:i+999);
end
end

#### 0 Comments

Sign in to comment.

## 1 Answer

### David K. (view profile)

Answer by David K.

### David K. (view profile)

on 26 Jul 2019
Accepted Answer

I believe your goal can be more easily solved using the built in Matlab function reshape. It looks to me like you have a 1x300000 vector that you would like to turn into a 1000x300 array. reshape function can do that like this :
store = reshape(RLF,[1000,300]);
If you wish to be sure that this is working the way you want it to, you can test it by making RLF 1:300000 first.
If instead, you want to continue doing it with a loop the following code should work fine
store = zeros(1000,300);
for n = 1:300
store(:,n)=RLF(1+(n-1)*1000 : n*1000);
end
The indexing there causes the indexes to go from 1:1000, 1001:2000, 2001:3000 and so on.
Hope this helps

Andrea Bae

### Andrea Bae (view profile)

on 26 Jul 2019
This works perfectly!! Thank you so much!!!!!!

Sign in to comment.