필터 지우기
필터 지우기

How to store a matrix output of a loop?

조회 수: 3 (최근 30일)
azarang asadi
azarang asadi 2020년 2월 1일
댓글: azarang asadi 2020년 2월 2일
Here's my code:
(tryna get the transformation matrices of each link of a robot and save them)
g=zeroes(4); %initial global frame
for j=1:7
theta=q(j);
alpha=al(j);
LinkLength=a(j);
LinkOffset=d(j);
g(j,:) = [cos(theta),-sin(theta)*cos(alpha),sin(theta)*sin(alpha),LinkLength*cos(theta);
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),LinkLength*sin(theta);
0,sin(alpha),cos(alpha),LinkOffset;
0,0,0,1];
end
I need to have 7 matrices corresponding to the transformation of each link like g(1),g(2), ....

채택된 답변

Walter Roberson
Walter Roberson 2020년 2월 1일
편집: Walter Roberson 2020년 2월 1일
g=zeroes(4, 4, 7); %initial global frame
for j=1:7
theta=q(j);
alpha=al(j);
LinkLength=a(j);
LinkOffset=d(j);
g(:, :, j) = [cos(theta),-sin(theta)*cos(alpha),sin(theta)*sin(alpha),LinkLength*cos(theta);
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),LinkLength*sin(theta);
0,sin(alpha),cos(alpha),LinkOffset;
0,0,0,1];
end
Now g(:,:,k) will be the k'th matrix.
If you prefer,
g = cell(7, 1); %initial global frame
for j=1:7
theta=q(j);
alpha=al(j);
LinkLength=a(j);
LinkOffset=d(j);
g{j} = [cos(theta),-sin(theta)*cos(alpha),sin(theta)*sin(alpha),LinkLength*cos(theta);
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),LinkLength*sin(theta);
0,sin(alpha),cos(alpha),LinkOffset;
0,0,0,1];
end
and then g{k} would be the k'th matrix.
  댓글 수: 1
azarang asadi
azarang asadi 2020년 2월 2일
first one didn't work but the second one worked, thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by