On Sun, May 1, 2011 at 7:54 PM, shay
<address@hidden> wrote:
I have a variable which is in a while loop and is created several times, with
the variable being given a different value at the end of each loop.
How can I add the variable into an array at the end of each loop without its
value changing when the loop repeats. In other words, I do not want the
variables' values to change once they are in the array, I merely wish to
concatenate the same variable into the array at the end of each loop
multiple times.
Thanks in advance,
Shay.--
I'm not sure if this is exactly what you want, but what I'm getting from the description is you have something like this:
xx = 1;
while (xx < 10)
xx = 2xx;
endwhile
And you want to save the result of xx every time the while loop condition is checked in an array?
If so, then the easiest (though least efficient) is to simply append the value to the end of an array, something like this (the results are saved in the array yy),
xx = 1;
yy = [];
while (xx < 10)
xx = 2*xx;
yy(end+1) = xx;
endwhile
This will simply grow the yy variable as more loops are executed, which could become time consuming as reallocation takes longer as yy grows. A more efficient, but one that depends on you setting a preset limit to the number of loops that the while loop will execute is:
xx = 1;
yy = zeros(max_loops, 1);
counter = 0;
while (xx < 10)
xx = 2*xx;
++counter;
yy(counter) = xx;
endwhile
Hope this helps.
James Sherman