From: brufl-andi on
Hey,

I have written the following function which contains a lot of loops. Now I want to vectorize it in terms of improving the performance of the program. In general the created structure array contains 9 matrices. Every entry of the matrix is calculated by the function sim_cpv_elem_vec, whereas p and q are points and not vectors!

It would be great if anybody has a general idea how to create a system of matrices whereas every single entry has to be calculated by a complex function.


function [C] =create_sim_defl( vol )

%input values:
% - vol.cond containing the conductivities of the three compartments
% - vol.bnd(k).pnt is a Nx3 matrix which contains nodal points of a triangulated surface
% - vol.bnd(k).tri is the corresponding Mx3 matrix which contains the indeces line by line of the nodal points representing a triangle on the surface


Omega=0;
p_defl=1/size(vol.bnd(ncmp).pnt,1); %addend which is needed for the deflation technique

%preallocation

for k=1:3
for l=1:3
k_size=size(vol.bnd(k).pnt); %the size of each matrix is determined by the number of vertices on
l_size=size(vol.bnd(l).pnt);
C(k,l).mat=zeros([k_size(1),l_size(1)]);% preallocating the 3x3 structure array
C(k,l).size=[k_size(1),l_size(1)];

end
end


for k=1:3
for l=1:3
prefac=(1/(2*pi))*((vol.cond(l)-vol.cond(l+1))/(vol.cond(k)+vol.cond(k+1))); % this is a constant factor depending only on k and l!
for p=1:C(k,l).size(1)
for q=1:C(k,l).size(2)
b=sim_cpv_elem_vec(p,q,vol.bnd(k).pnt,vol.bnd(l).pnt, vol.bnd(l).tri);
if l==3
C(k,l).mat(p,q)=prefac*b-p_defl;
else
C(k,l).mat(p,q)=prefac*b;
end
end
end

end
end