Cell Array and Matrix Multiplication

5 ビュー (過去 30 日間)
Brian
Brian 2011 年 2 月 17 日
Here is my question/problem:
I need to multiply several 4x4 matrices together in order based on some data. I have a loop that constructs a 1 x n cell array with each cell being a 4x4 matrix.
for i = 1:n
ct = cos(dh.t(i+1));
st = sin(dh.t(i+1));
cf = cos(dh.f(i));
sf = sin(dh.f(i));
a = dh.a(i);
d = dh.d(i+1);
A = [ct , -st, 0, a;
cf*st, cf*ct, -sf, -sf*d;
sf*st, sf*ct, cf, cf*d;
0 , 0, 0, 1];
B(1,i) = {A};
end
A is the matrix for each iteration i and is stored in the cell array B. I need each of the matrices in B to be multiplied by each other in order and return a single 4x4 matrix ( i.e. T = B{1,1}*B{1,2}*B{1,3}*...*B{1,n} ). I am not sure how to do this for a general case n. I am not even sure if what I did above is the best way to do this problem, but that's what I have done so far.
Thanks for any help.

採用された回答

the cyclist
the cyclist 2011 年 2 月 17 日
If everything is numeric, do you really need to use cell arrays (which then need to be "unpacked" to do the numeric calculation again)? Or could you do something like:
B = zeros(4,4,n);
T = eye(4);
for i = 1:n
blah blah
B(:,:,i) = A;
T = T*B(:,:,i);
end
It is not clear what, if any, of the intermediate results you need to ultimately store, so you may not even to keep B at all.
  1 件のコメント
Brian
Brian 2011 年 2 月 17 日
Awesome. This is much better than what I was trying to do. Thanks for the help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by