Vectorized rotate() slower than loop?
2 views (last 30 days)
Show older comments
Hi all,
I have an array of patch objects in a scene. I'd like to rotate each object about the z-axis, by some arbitrary angle (different for each patch), about some arbitrary origin (again different for each).
Of course, looping through rotate(handle, axis, angle, origin) is slower than I'd like. So I've been trying to vectorize the operation. But my speed tests seem to suggest this makes things slower! I'm thinking I might not be correctly vectorizing the angle/origin somehow. Anyway, here's my speed test:
% Grab the first 10 patch objects and rotate
c = patch_handles(1:10);
tic
for i=1e3 % Do this 1,000 times
for j=1:10 % Rotate each patch separately
rotate(c(j),[0 0 1],rand,rand(1,3));
end
end
toc
d = patch_handles(1:10);
tic
for i=1:1e3 % Do this 1,000 times
% Rotate all 10 patches at once
rotate(d,[0 0 1],rand(10,1), rand(10,3));
end
toc
......
Result:
Elapsed time is 0.130681 seconds.
Elapsed time is 2.099411 seconds.
0 Comments
Answers (3)
Matt J
on 26 Mar 2014
Edited: Matt J
on 26 Mar 2014
Your comparison is corrupted because the first implementation is only looping over a single value i=1e3 whereas your second version loops over a thousand values 1:1e3.
If you have lots of patches to do, you should probably also set the figure invisible until all patches are rotated. Otherwise, a re-rendering of the entire figure will be done after each rotation.
0 Comments
Matt J
on 26 Mar 2014
Edited: Matt J
on 26 Mar 2014
You could also try this FEX file,
If you extract all of your vertex coordinates for all the patches into a common matrix 3xN matrix XYZ_old, you can then use Syntax 3 of the above file. This will rotate all of the coordinates in a vectorized way. Then you build new patches out of the new coordinates.
0 Comments
See Also
Categories
Find more on Lighting, Transparency, and Shading 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!