1

I am trying to get the transformation matrices from DH parameter table and would like to save each matrix (Ai) getting after each iteration of i. How can I do that?

DH = {{0, T1, 0, 0}, {0, T2, L1, Pi/2}, {0, T3, L2, 0}, {0, 0, L3, 0}} 
For[i = 1, i <= 4, i++, 
A = {{Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, DH[[i, 3]]}, 
{Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], Cos[DH[[i, 2]]] Cos[DH[[i, 4]]],Sin[DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, 
 {Sin[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 4]]],Cos[DH[[i, 4]]] DH[[i, 1]]}, {0, 0, 0, 1}}; 
Print["A = ", MatrixForm[A]];]
pinkpanta
  • 23
  • 2
  • 2
    Take a look at Table and see https://mathematica.stackexchange.com/questions/134609/why-should-i-avoid-the-for-loop-in-mathematica – Szabolcs Oct 11 '19 at 18:26
  • 1
  • {{Cos[DH[[#, 2]]], -Sin[DH[[#, 2]]], 0, DH[[#, 3]]}, {Sin[DH[[#, 2]]] Cos[DH[[#, 4]]], Cos[DH[[#, 2]]] Cos[DH[[#, 4]]], Sin[DH[[#, 4]]], -Sin[DH[[#, 4]]] DH[[#, 1]]}, {Sin[ DH[[#, 2]]] Sin[DH[[#, 4]]], Cos[DH[[#, 2]]] Sin[DH[[#, 4]]], Cos[DH[[#, 4]]], Cos[DH[[#, 4]]] DH[[#, 1]]}, {0, 0, 0, 1}} & /@ Range[4] – Bob Hanlon Oct 11 '19 at 18:57

1 Answers1

1

Here are two ways to do what you want. Which one you should choose depends on how you want to refer to the matrices produced in further calculations. If you use the 1st way, you would refer to matrices as indexed variables, A[...]. If you use the 2nd way, you would refer to them as list items, A[[...]]

DH = {{0, T1, 0, 0}, {0, T2, L1, Pi/2}, {0, T3, L2, 0}, {0, 0, L3, 0}};
  • Indexed variables
Clear[A]
Do[
  A[i] = 
    {{Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, DH[[i, 3]]}, 
     {Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], Cos[DH[[i, 2]]] Cos[DH[[i, 4]]], 
      Sin[DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, 
     {Sin[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], 
      Cos[DH[[i, 4]]], Cos[DH[[i, 4]]] DH[[i, 1]]}, 
     {0, 0, 0, 1}};
  {i, Length[DH]}]
Column[Table[MatrixForm[A[i]], {i, Length[DH]}]]

table

  • List of matrices
Clear[A]
A =
  Table[
    {{Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, DH[[i, 3]]}, 
     {Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], Cos[DH[[i, 2]]] Cos[DH[[i, 4]]], 
      Sin[DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, 
     {Sin[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], 
      Cos[DH[[i, 4]]], Cos[DH[[i, 4]]] DH[[i, 1]]},
     {0, 0, 0, 1}},
    {i, Length[DH]}];
Column[Table[MatrixForm[A[[i]]], {i, Length[A]}]]

table

m_goldberg
  • 107,779
  • 16
  • 103
  • 257