storing output of for loop

1 次查看(过去 30 天)
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

更多回答(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