Matlab Storing Vector after Iterations

152 Views Asked by At

I am having serious issues storing information in a vector after every iteration. Suppose that we have this code;

delta = 0;
figure(1)
while delta < 30
[t,x_]=Ass3(10,8/3,10+delta,100,1,1,1)
hold on
plot3(x_(:,1),x_(:,2),x_(:,3));
grid on
title('Changing Rho on x,y,z plane')
xlabel('x Value')
ylabel('y Value') zlabel('z Value')
delta = delta + 5
end

Where Ass3 is another function from a different document. I want to store a vector after every incremented 'delta' I have attempted to use an if statement with lengths being less then the length of a different vector but nothing seems to be working.

1

There are 1 best solutions below

0
On BEST ANSWER

So before actually answering the question, i would recommend a slight edit for your code. As you know how the variable delta will change (starting at zero and increasing in steps of 5 as long as delta < 30), i would replace the while by a for loop.

If you know the length of the vector you want to store after each iteration (let's call this length l) then i would recommend the following:

delta = 0:5:25;
iterations = length(delta);
data = zeros(iterations,l)
figure(1)
for i = 1:iterations
  ...
  data(i,:) = YourVectorToSave;
end

The data array is preallocated here which is the best thing to do when you know how much iterations will be performed and how much data will be stored in each iteration.

If the size of the vector changes between different iterations (or the data is not a vector of doubles), i am not quite sure what i would call the best solution. As I would always recommend to avoid objects with changing size, a possible solution could be a cell array. This would lead to

delta = 0:5:25;
iterations = length(delta);
data = cell(1,iterations)
figure(1)
for i = 1:iterations
  ...
  data{i} = YourVectorToSave;
end