MATLAB Answers

storing output of for loop

4 views (last 30 days)
Andrea Bae
Andrea Bae on 26 Jul 2019
Commented: Andrea Bae on 26 Jul 2019
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.

Accepted Answer

David K.
David K. on 26 Jul 2019
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

  1 Comment

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

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags