I would appreaciate any help vectorizing the for loop below. The problem i cannot solve is how to take out the constants from wdrive and multiply it with the rest, according to the loop below. I want to become better at vectorizing my MATLAB code so I would really appreciate any help! Thanks alot!
%S and R are square matrices (404*404). %fc is a column vector
wdrive=0:1:800; a2=zeros(2*dofs,(length(wdrive))); b2=zeros(2*dofs,(length(wdrive))); Amp=zeros(1,length(wdrive));
for i=1:length(wdrive) a2(:,i)=(wdrive(i)^2*SRS+R)\(fs-wdrive(i)*SRfc); b2(:,i)=(R)\(fc+wdrive(i)*S*a2(:,i)); Amp(i)=sqrt(a2(dofs-1,i)^2+b2(dofs-1,i)^2); end
No products are associated with this question.
I don't think you can vectorize everything, but you can cut down on the operations done inside the loop,
Rfc=R\fc; SRS=S*(R\S); SRfc=S*(Rfc);
for i=1:length(wdrive) a2(:,i)=(wdrive(i)^2*SRS+R)\rhs(:,i); end
b2=Rfc+R\S*bsxfun(@times, wdrive, a2); Amp=abs(a2(dofs-1,:)+i*b2(dofs-1,:));