Vectorized rotate() slower than loop?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
답변 (3개)
Matt J
2014년 3월 26일
편집: Matt J
2014년 3월 26일
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
Matt J
2014년 3월 26일
편집: Matt J
2014년 3월 26일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!