Matrix multiplication (best computational approach)
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have to make a coordinates transformation between two reference systems (axes). For it, three matrices (3x3) have to be multiplied due to some intermediate axes are used. I have thought two approaches to resolve this:
method #1: Making the multiplication directly, i.e. v_f = R1*R2*R3*v_i
method #2: Split into steps.
step 1: v_3i = R3*v_i
step 2: v_23 = R2*v_3i
step 3: v_f = R1*v_23
where: R1, R2 and R3 are 3x3 matrices
v_f,v_i, v_3i, v_23 are 3x1 vectors
I would like to know what method is more efficient computationally (less time) to do the transformation (this will be made a lot of times).
0 Kommentare
Akzeptierte Antwort
Teja Muppirala
am 14 Mai 2012
I think you might be better off grouping your multiplications so that you maximize matrix-vector products instead of matrix-matrix products.
For example:
R1 = rand(3);
R2 = rand(3);
R3 = rand(3);
v = rand(3,1);
tic;
for n = 1:1e6, R1*R2*R3*v; end;
toc;
% Slightly faster
tic;
for n = 1:1e6, R1*(R2*(R3*v)); end;
toc;
This is especially true for larger matrices much bigger than 3x3.
R1 = rand(1000);
R2 = rand(1000);
R3 = rand(1000);
v = rand(1000,1);
tic;
R1*R2*R3*v;
toc;
% Much faster
tic;
R1*(R2*(R3*v));
toc;
Of course if the R's are all constant and you are reusing them, then you should just multiply them all together and store that result to begin with.
3 Kommentare
Jan
am 14 Mai 2012
R3*v replies a vector such that forcing the execution to start from the right is more efficient than the accumulating the rotation matrices at first.
Weitere Antworten (2)
Jan
am 14 Mai 2012
There is no difference internally. The intermediate results have to be calculated in both cases. A TIC/TOC measurement allows you to find out, which one is faster.
Usually it is possible to calculate R1*R2*R3 explicitely without a matrix multiplication.
[EDITED, appended after your comment]:
function R = RotMatrix(N, A)
% N: [1 x 3], normalized vector to rotate around
% A: [1 x 1], angle
% Calculate trigonometric values once to save time:
cA = cos(A);
sA = sin(A);
cmA = 1 - cA;
x = N(:, 1);
y = N(:, 2);
z = N(:, 3);
ysA = y .* sA; % Some optimizations to save products
zsA = z .* sA;
xsA = x .* sA;
xcmA = x .* cmA;
ycmA = y .* cmA;
yzcmA = z .* ycmA;
xycmA = x .* ycmA;
xzcmA = z .* xcmA;
R = [cA + x .* xcmA, xycmA + zsA, xzcmA - ysA; ...
xycmA - zsA, cA + y .* ycmA, yzcmA + xsA; ...
xzcmA + ysA, xsA - yzcmA, cA + z .* z .* cmA];
For a real application it could be more efficient to accept [nFrame x 3] arrays as list of vectors and a [nFrame x 1] list of angles. Then the context decides, which output dimensions are most useful, perhaps a [3 x 3 x nFrame] array or three [3 x nFrame] matrices, which allow a DOT product directly to transform vectors.
3 Kommentare
Jan
am 14 Mai 2012
See: http://en.wikipedia.org/wiki/Rotation_matrix and http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions . Any rotation can be described by a 3x3 matrix, which represents a rotation around a specific axes in 3D, see "General rotation" and "Rotation from axes and angle".
James Tursa
am 14 Mai 2012
If you are simply after speed, the fastest approach will be a mex routine to do the calculations inline (i.e., explicitly without calling a separate generic matrix multiply package ala BLAS). This avoids all of the forming of intermediate variables. How many is "... lots of times"? Which variable(s) are changing in each iteration of the loop?
3 Kommentare
James Tursa
am 14 Mai 2012
So *all* of the matrices are changing and are formed at each iteration? How do you form them? With trig functions of angles for each element of the matrix? If so, rather than forming the matrices at the m-file level it would also be a speed benefit to simply pass the angles into the mex routine directly and have the mex routine calculate the matrix elements on the fly as well as do the matrix multiplies on the fly. Can you give a short section of your code that gives the angle inputs and your matrix formation?
Siehe auch
Kategorien
Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!