Hi all,
I have a complicated problem about "for loop" and "matrix", I would like to have some helps from you.
Let's X1 be a vector with length n1, X2 be a vector with length n2, F1 be a N1xn1 matrix, F2 be a N2xn2 matrix, and Q be a N1xN2 matrix. I like compute the matrix Q as follows:
for i1=1:N1for i2=1:N2 Q(i1,i2)=Q(i1,i2)+trapz(X2(p2(1):p2(3)),trapz(X1(p1(1):p1(3)),Q(p1(1):p1(3),p2(1):p2(3))... .*repmat(F1(i1,p1(1):p1(3))',1,p2(3)-p2(1)+1)).*F2(i2,p2(1):p2(3)));endend
where p1 and p2 are index vectors.
This program with "for loop" gives me a good result but takes time. I would like to transform it into "matrix" form to accelerate the computation.
The problem is that the output Q is taken as input in the body of "for loop". If it is not taken as the input, I can transform the above "for loop" as follows:
I=squeeze(trapz(X1(p1(1):p1(3)),repmat(Q(p1(1):p1(3),p2(1):p2(3)),[1 1 N1])... .*rot90_3D(rot90_3D(repmat(F1(:,p1(1):p1(3)),[1 1 p2(3)-p2(1)+1]),3,3),1,3)));Q=Q+squeeze(trapz(X2(p2(1):p2(3)),repmat(I,[1 1 N2]).*rot90_3D(rot90_3D(repmat(F2(:,p2(1):p2(3)),[1 1 N1]),3,3),1,3)));
It works but it is not exactly what I want.
So, do you have any idea to deal with the problem? Thank you in advance for you help.
Tuan
Best Answer