Efficiency of a symbolic math loop
Show older comments
Hi all,
I have a piece of code I intend to run in real time with a camera and consequently I want to optimise everything as well as I can. The Symbolic Toolbox is something new to me, hopefully someone can point out where I might be able to increase the efficiency of the code.
The code is a little module I made for ray casting in another part of my project. Using the Sym math to get the 3D point of intersection between a ground plane and a ray from a camera. The main slowdown is in the for loop at the bottom.
Cheers guys
Code is:
P1 = [1,-1,0.1];
P2 = [2,3,0.1];
P3 = [-5,6,0.2]; %3 points define a plane
cameraPosition = [0 0 0.5];
pixel = [1:640;(ones(640,1)*320)']'; %Line of pixels from camera
totalOps = length(pixel(:,1));
normal = cross(P1-P2, P1-P3);
syms x y z xL yL zL m;
P = [x,y,z];
planefunction = dot(normal, P-P1);
horFOV = deg2rad(57);
vertFOV = deg2rad(43);
horGaps = horFOV/640; %rad/pixel
vertGaps = vertFOV/480; %rad/pixel
alpha = (-pixel(:,2)+240)*vertGaps;
theta = (-pixel(:,1)+320)*horGaps+pi/2;
PL = [cameraPosition(1)+1*sin(theta), cameraPosition(2)-1*cos(theta), cameraPosition(3)+1*sin(alpha)];
R = PL-repmat(cameraPosition,totalOps,1);
intercept = zeros(3,totalOps);
digits(6);
for i=1:totalOps
xL = PL(i,1)+R(i,1)*m;
yL = PL(i,2)+R(i,2)*m;
zL = PL(i,3)+R(i,3)*m;
subF = vpa(subs(planefunction,{x,y,z},[xL,yL,zL]));
subFcoeffs = double(coeffs(subF));
mVar = linsolve(-subFcoeffs(2),subFcoeffs(1));
intercept(:,i) = PL(i,:)+R(i,:)*mVar;
end
1 Comment
Walter Roberson
on 28 Sep 2012
You should probably be considering using matlabFuntion() instead of vpa(subs())
Accepted Answer
More Answers (0)
Categories
Find more on Classical Control Design in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!