MATLAB: Saving for loop output in an array

arrayfor loop

I wrote a code in which I predefine the variable "a" and then set up a for loop of 5 iterations where the variable "a" goes through some basic operations. However, the for loop output only saves the fifth iteration of "a." How do I save all 5 iterations in a 1×5 array?
The code is as follows:
a = 10;
k = 0.5;
n = 2;
for m = 1:5
a = a + (a*k) + n;
end

Best Answer

Hi,
You will have to insert the value of a in another array during each iteration. Below is the modified code:
a = 10;
k = 0.5;
n = 2;
valueOfA = zeros(1,5);
for m = 1:5
a = a + (a*k) + n;
valueOfA(m) = a;
end