MATLAB: Getting rid of for loop in one-liner code

optimizationvectorization

Lets assume we have an optimization problem like this:
x = zeros(1,10);
x(1,1) = 2;
for k = 1: 9
x(k+1) = 10 x(k);
end
Is it possible to write the equation without the for loop?

Best Answer

Sakil
ok for a matrix, look:
A=[1 2;3 4];n=[1:1:10];L=A(:);B=reshape(bsxfun(@power,L,n),[size(A),10])
the resulting exponential matrices are stored in the layers of B
B(:,:,1) =
1 2
3 4
B(:,:,2) =
1 4
9 16
B(:,:,3) =
1 8
27 64
B(:,:,4) =
1 16
81 256
B(:,:,5) =
1 32
243 1024
B(:,:,6) =
1 64
729 4096
B(:,:,7) =
1 128
2187 16384
B(:,:,8) =
1 256
6561 65536
B(:,:,9) =
1 512
19683 262144
B(:,:,10) =
1 1024
59049 1048576
.
Sakil
would you please be so kind to mark my answer as Accepted Answer,
thanks in advance
regards
John BG