storing output of for loop

조회 수: 5 (최근 30일)
Andrea Bae
Andrea Bae 2019년 7월 26일
댓글: Andrea Bae 2019년 7월 26일
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

채택된 답변

David K.
David K. 2019년 7월 26일
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
Andrea Bae
Andrea Bae 2019년 7월 26일
This works perfectly!! Thank you so much!!!!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by