Here is piece of Matlab code. It works very slow. Is there any way to make it work faster? I cant figure out the way to vectorize it.Maybe it can be written like some kind of filter ?
...
for uu=2:length(x)-2;
for vv= 2:length(y)-2;
P1=[x(uu+1) y(vv) temp(uu+1,vv)];
P2=[x(uu) y(vv+1) temp(uu,vv+1)];
P3=[x(uu-1),y(vv) temp(uu-1,vv)];
P4=[x(uu) y(vv-1) temp(uu,vv-1)];
cr=cross((P1-P3),(P2-P4));
cr=cr/norm(cr);
theta=acos(dot([0,0,1],px))*180/pi;
...
end
end
...
To vectorize this nested loop, you need to reshape your data properly. The cross product can be performed along a certain dimension, so all you really need to do is to reshape P1 … P4 into vectors containing your data well ordered.
Notice that you have a “cross-shaped” kernel for your operation. P1 could be the bottom part, P3 the top part, P2 the right part and P4 the left part. Assuming that the vectors x and y are only the coordinates of sampled points (temp), each of those vectors could be represented like this :
Theta doesn’t seem to be dependent of any variables in your loop, but acos(X) and dot(v1,v2) can be used with vectorized data, same as cross(v1,v2).
I doubt that you could get any performance boost from decomposing the cross product function in its cofactor expansion, or by trying to implement it as some kind of non-linear filter. If it’s still too slow, you should have a look at the Parallel toolbox.
Hope this helps !