How to vectorize matrixvector multiplications with previous-dependent input?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Most of the operations can be moved out of the loop:
t = ones(2,K);
B = [cos(Fi),-sin(Fi);sin(Fi),cos(Fi)];
for k = 2:K
t(:,k) = B(:,:,k) * t(:,k-1);
end
t(:,1) = 0;
dna = cumsum(t,2);
카테고리
도움말 센터 및 File Exchange에서 Biomechanics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!