question abt variable size array in a for loop
Show older comments
Hi there,
I am working with an ODE solver inside a for loop and am trying to save every iteration in a variable. The thing is because of some initial conditions which are generated by a random number generator, the final integrated solution has a variable size. I need every iteration to be saved to a variable but have run out of ideas. Tried preallocating as
num=5; X = zeros(10000,6,num);
and also by updating the variable X every iteration as
XX = []; for j = 1:num new_X = X len = length(X)
for kkk = 1:len
TT = zeros(len,j);
XX(:,:) = [XX new_X];
end
Thanks
Answers (1)
Andrew Newell
on 20 Jan 2012
Use cell arrays. If I understand your problem, you are saving num runs, so you could use code like this:
XX = cell(num,1);
for j = 1:num
XX{j} = new_X;
end
If you haven't used cell arrays before, you would be well advised to read the documentation. In particular, to access the contents of a cell, use curly brackets.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!