MATLAB: Generalization of nth index

indexing

I want to create a 1*n vector A, where each successive element is square of previous. Now if i write A(n)=(A(n-1))^2 Matlab takes n as the last element. Is there a way to generalize n? Or any other way to get such a vector? Thanks.

Best Answer

It looks like you’re doing this in a loop. The solution is to add one to the vector in each iteration:
N = 5;
A = 2;
for n = 1:N
A(n+1) = A(n)^2;
end
Note that unless you begin with some value greater than 1, you end up with ‘A=[1,1,1,...]’.