how to Vectorize iterative '' coordinates transformation ''loop operation for speed

I want to transform local coordinates(tip positions) of a rotating blade into corresponding global coords. Tip coordinates and angular distance changes in time. which means there is a unique transformation matrix for every time step. hence i used loop to multiply transformation matrix (3*3) with local coords to get me global coords each time step. for accuracy reasons i need more than million transformations,which take more than 5 hours with my core i5 computer.its a 2D transformation. i need to vectorize only following part of the code.Please check attachment for complete code.
for i = 1: vec_size
M=[cos(theta_inst(i)) -sin(theta_inst(i)) dx(i);...
sin(theta_inst(i)) cos(theta_inst(i)) dy(i); ...
0 0 1]; %transformation matrix
glbl(1:3,i)= M*local(:,i); % dx,dy ~=0;
end

 採用された回答

Matt J
Matt J 2018 年 9 月 7 日
編集済み: Matt J 2018 年 9 月 7 日
In non-homogeneous coordinates,
c=cos(theta_inst(:)).'; s=sin(theta_inst(:)).';
dx=dx(:).'; dy=dy(:).';
L=local(1:2,:);
glbl = [ sum([c;-s].*L) ; sum([+s;c].*L) ]+[dx;dy] ;

4 件のコメント

kamal wisal
kamal wisal 2018 年 9 月 7 日
Perfect...Thank you @Matt J. I compared results of both loop and vectorized operations. there is a difference of order 2^-16 to 2^-18 in both results. Is it because of using SUM ftn??? may be sum ftn will round off digits and hence creating a difference?
Matt J
Matt J 2018 年 9 月 7 日
Relative error?
kamal wisal
kamal wisal 2018 年 9 月 8 日
yes. difference of outputs (global coords) as calculated by iterative process (for loop) and by vectorized operation using sum function.
Matt J
Matt J 2018 年 9 月 10 日
If it's relative error, it is likely due simply to floating point errors.

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2018a

質問済み:

2018 年 9 月 7 日

コメント済み:

2018 年 9 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by