Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: vectorize this
Replies: 4   Last Post: Mar 6, 2012 7:02 PM

 Messages: [ Previous | Next ]
 Marios Karaoulis Posts: 89 Registered: 5/10/10
vectorize this
Posted: Jan 31, 2012 2:57 PM

Hello all,

Could you help me vectorize the following code? Ideally, I need to
remove the internal for loop.

num_mes=1890;
num_param=225;
Jx=rand(num_param,94221);
Jy=rand(num_param,94221);
Jz=rand(num_param,94221);

jac=zeros(num_mes,num_param);
ind_data=randi(num_mes,num_mes,4);

jtmp=randi(94221,500,num_param);

for i=1:num_mes

A=ind_data(i,1);
B=ind_data(i,2);
M=ind_data(i,3);
N=ind_data(i,4);

JA = [Jx(A,:) ;Jy(A,:); Jz(A,:)].';
JA = reshape(JA(mesh.T,:),[],4,3);

JM = [Jx(M,:); Jy(M,:) ;Jz(M,:)].';
JM = reshape(JM(mesh.T,:),[],4,3);

JN = [Jx(N,:) ;Jy(N,:) ;Jz(N,:)].';
JN = reshape(JN(mesh.T,:),[],4,3);

JB = [Jx(B,:) ;Jy(B,:); Jz(B,:)].';
JB = reshape(JB(mesh.T,:),[],4,3);

J_all=dot(JA-JB,JM-JN,3);
tmp_jac=mean(J_all,2);

for k=1:num_param
jtmp_this_iter =nonzeros( jtmp(:,k));
jac(i,k)=sum(tmp_jac(jtmp_this_iter));
end

end

Date Subject Author
1/31/12 Marios Karaoulis
2/1/12 Bruno Luong
3/6/12 Marios
3/6/12 Roger Stafford
3/6/12 Marios