DH is a 7*5 double.
The values in each row of DH are used for calculation in each loop.
To speed up, I've used pre-location. But can it be faster with vectorization?
DH = psm_m.DH;
n = size(DH,1);
T_i_All = zeros(4,4,n);
for i = 1:n
T_i_All (:,:,i)= [cos(DH(i,5)) -sin(DH(i,5)) 0 DH(i,3);
(sin(DH(i,5)))*(cos(DH(i,2))) (cos(DH(i,5)))*(cos(DH(i,2))) -sin(DH(i,2)) -(sin(DH(i,2)))*DH(i,4);
(sin(DH(i,5)))*(sin(DH(i,2))) (cos(DH(i,5)))*(sin(DH(i,2))) cos(DH(i,2)) (cos(DH(i,2)))*DH(i,4);
0 0 0 1];
end
I followed the MATLAB example
t = 0:.01:10; %example
y = sin(t); %example
But it doesn't work.
i=1:n;
T_i_All (:,:,i)= [cos(DH(i,5)) -sin(DH(i,5)) 0 DH(i,3);
(sin(DH(i,5)))*(cos(DH(i,2))) (cos(DH(i,5)))*(cos(DH(i,2))) -sin(DH(i,2)) -(sin(DH(i,2)))*DH(i,4);
(sin(DH(i,5)))*(sin(DH(i,2))) (cos(DH(i,5)))*(sin(DH(i,2))) cos(DH(i,2)) (cos(DH(i,2)))*DH(i,4);
0 0 0 1];
(Error using horzcat
Dimensions of matrices being concatenated are not consistent.)
Any help? Thanks!

 채택된 답변

Guillaume
Guillaume 2018년 12월 18일

1 개 추천

First, permute DH so that the rows are in the 3rd dimension as in your output. Then you can vectorise your calculation:
DH = permute(psm_m.DH, [3 2 1]);
n = size(DH, 3);
T_i_All = [cos(DH(1, 5, :)), -sin(DH(1, 5, :)), repmat(0, 1, 1, n), DH(1, 3, :);
sin(DH(1, 5, :)).*cos(DH(1, 2, :)), cos(DH(1, 5, :)).*cos(DH(1, 2, :)), -sin(DH(1, 2, :)), -sin(DH(1, 2, :)).*DH(1, 4, :);
sin(DH(1, 5, :)).*sin(DH(1, 2, :)), cos(DH(1, 5, :)).*sin(DH(1, 2, :)), cos(DH(1, 2, :)), cos(DH(1, 2, :)).*DH(1, 4, :);
repmat([0 0 0 1], 1, 1, n)]

댓글 수: 5

madhan ravi
madhan ravi 2018년 12월 18일
+1 amazing!
Laurel Castillo
Laurel Castillo 2018년 12월 18일
agree, thanks so much!! First time to see permute and repmat function!
now T_i_All is 4*4*7, can we also obtain [T_i_All(:,:,1); T_i_All(:,:,:1)*T_i_All(:,:,:2); T_i_All(:,:,:1)*T_i_All(:,:,:2)*T_i_All(:,:,:3) ......]?
Guillaume
Guillaume 2018년 12월 18일
Is your multiply a matrix multiplication or an element-wise multiplication?
Laurel Castillo
Laurel Castillo 2018년 12월 18일
matrix multiplication!
Guillaume
Guillaume 2018년 12월 18일
편집: Guillaume 2018년 12월 18일
I don't see a way to vectorise that. You'll have to use a loop:
composition = T_i_all;
for page = 2:size(composition, 3)
composition(:, :, page) = composition(:, :, page) * composition(:, :, page-1);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2018년 12월 18일

편집:

2018년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by