0
votes

Now, I understand the problem here, what I don't understand however is that how should 'I' be initialized??

For instance, in my case variable 'p' generates an array at the end of every for loop, which is programmed to run for 101 times. The output of p looks something like this

p =

-0.0149 -0.0149 Now, I want to store this value into another variable at the end of every for loop and increment its index.

So,What should be the dimension of that variable?

I have tried initializing ---> A=rand(2,101);

1
Note that it's probably a bad idea to initialise anything with rand if that is not the intended data. The only two good initial values for most variables are 0 or NaN. Anything else is just a spurious guess. - wakjah

1 Answers

2
votes

If you don't wish to pre-allocate the array A. Then you can keep appending the values in A at the each for loop iteration as follows:

A=[];
for loop
   get p vector (every iteration it should be 2x1)
   A=[A p];
end

Or you can directly write in the for loop:

 A(:,i)=p; %but your p vector seems to be 1x2 and not 2x1. If it is 2x1, then you should initialize A as rand(101,2)