Fast dataset manipulation in MATLAB
Show older comments
Problem definition
I have a dataset composed by m 2-dimensional vectors, stored in a 2xm matrix D. Given a rotation angle theta (in rads) and given a 2-dimensional translation vector p, I have to compute a (inverse) roto-translation of each vectors in the dataset. I'm wondering what is the fastest solution to perform this task because I've noticed that in my code the bottleneck is, indeed, caused by the computation of a roto-translation (99.8% of the total computation time).
To fix the notation, I denote the generic roto-translation as the following affine transformation
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
bary = R' * (y - p);
where y is the original vector, bary is the roto-translated vector, R is the rotation matrix and p is the translation vector.
Naive solution
The simplest solution is to apply to each single vector in the dataset D the previous transformation
for j = 1 : m
barD(:, j) = R' * (D(:, j) - p);
end
where barD is the rototranslated dataset. Hence, here we perform m "small" affine transformations.
A (hopefully) better solution
I'm not an expert in programming, but I understand, if I'm not wrong, that we should avoid to use for cycles as much as possible because MATLAB is optimized to manipulate matrices and most of the times we can express for cycles as matrix manipulations. If I'm not wrong, this concept is called code vectorization. In this case we can replace the previous for cycle with a single "big" affine transformation. This objective can be achieved by stacking the dataset vectors in one single "big" 2*m x 1 vector vecD.
vecD = reshape(D, [2*m 1]);
Then, we can compute the m rototranslations via the following "diagonal" trasformation
invRR = kron(eye(m), R');
pp = kron(ones(m, 1), p);
vecbarD = invRR * (vecD - pp);
where vecbarD is the vectorial representation of the rototranslated dataset barD. To conclude the task, vecbarD must be converted back in the 2 x m matrix representation barD. So far I'm using the following code, but probably it would be better to use reshape once again.
xidx = 1 : 2 : 2 * m - 1;
yidx = 2 : 2 : 2 * m;
barD = [vecbarD(xidx)'; vecbarD(yidx)'];
Questions
- I would like to know if what I've written makes sense and where I can find some resource about code vectorization
- I would like to know if and how my solutions can be improved in terms of the computational time
- Since each vector in the dataset is treated independently, I would like to know how to parallelize my code. A trivial solution is to replace the for of my naive solution with a parfor, but I don't know if this strategy is better than a vectorized one.
Accepted Answer
More Answers (0)
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!