MATLAB: Indexing a vector by a power

vector

I would like my vector to go by powers of 2 but everything I tried hasn't worked.
for n = 0:9
pow = (1:2^n:512)
end
but this doesn't return a vector with 1, 2, 4, 16, …
What can I do to achieve this?

Best Answer

You could accomplish this without using a for loop as follows:
n = 4;
p = 2.^cumprod([1,2+zeros(1,n-1)])
this results in:
p =
2 4 16 256
*Mind you, the start of the sequence you are asking for doesn't follow your own rules. Yes each element is the square of the previous one, except that 1^2 does not equal 2. So if you are intent on breaking that rule and want this sequence to start with a 1 and have n elements, then you would need to do this:
p = [1,2.^cumprod([1,2+zeros(1,n-2)])]
yielding:
p =
1 2 4 16