MATLAB: How to vectorize matrixvector multiplications with previous-dependent input

matrixmultiplicationvectorvectorization

How to avoid the for loop in the following code, creating a 2D polymer structure with a new random created bending angle of the following segment:
N=10;
K=200;
Xi=50;
l=0.311;
Fi=normrnd(0,sqrt(l/Xi),[1,1,K])
dna=zeros(2,K);
t=ones(2,K);
for i=2:K
A=[cos(Fi(i)) -sin(Fi(i));sin(Fi(i)) cos(Fi(i))]
t(:,i)=A*t(:,i-1);
dna(:,i)=dna(:,i-1)+t(:,i);
end

Best Answer

The for-loop can be eliminated, but I doubt that it will save you any time.
K = 200;
Xi = 50;
l = 0.311;
t = [rand(2,1),zeros(2,K-1)]; % <-- You choose t(:,1)
Fi = normrnd(0,sqrt(l/Xi),[1,1,K])
Gi = cumsum(reshape(Fi(1,1,2:K),1,[]),2); % cumsum the angles in Fi
sG = sin(Gi);
cG = cos(Gi);
t(1,2:K) = cG*t(1,1)-sG*t(2,1);
t(2,2:K) = sG*t(1,1)+cG*t(2,1);
dna = [zeros(2,1),cumsum(t(:,2:K),2)];