Efficiency of a symbolic math loop

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 Kommentar

Walter Roberson
Walter Roberson am 28 Sep. 2012
You should probably be considering using matlabFuntion() instead of vpa(subs())

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 28 Sep. 2012
Bearbeitet: Andrei Bobrov am 28 Sep. 2012

0 Stimmen

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);
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);
xyzL = PL + R*1i;
subF = bsxfun(@minus,xyzL,P1)*normal.';
mVar = real(subF)./-imag(subF);
intercept = (PL + bsxfun(@times,R,mVar)).';

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by