フィルターのクリア

Array of Matrices multiplication

20 ビュー (過去 30 日間)
Hugo Hernández Hernández
Hugo Hernández Hernández 2020 年 12 月 9 日
Solving an exercise for orbital mechanics I found a problem while multiplying a 3x3 matrix by a 3x1 Matrix, actually these matrices have 1442 values so first one is 3x4326 and second one is 3x1442. First array is a vector of 3x3 matrices but I do not know how to separate each matrix and then multiply them by my 3x1 array of matrices. I have tried with a for loop, or by selecting the arrays or columns and then compute the operation but that did not work. The full code is very large and has other .m files involved so I am attaching the specific lines with the problem:
%% Position
R3_Thot = [cos(Tho_t), sin(Tho_t), z_0; -sin(Tho_t), cos(Tho_t), z_0; z_0, z_0, z_1];
%%% 3x3 array of matrices with 3x4326 length
%%% R_p1 is an array of 3x1 matrices with a 3x1442 length
Rpos_Efix1 = R3_Thot*R_p1; %%% I want to perform this operation but I got errors like size mismatching
I tried also with .* operator but does not work, and I am not sure how to solve or compute them using a for loop.

採用された回答

Adam Danz
Adam Danz 2020 年 12 月 9 日
編集済み: Adam Danz 2020 年 12 月 9 日
One way to approach this is to break up the 3x4326 matrix into a 3x3x1442 array.
data = rand(3,4326); % 3x4326 matrix
dataArray = reshape(data,3,3,size(data,2)/3);
size(dataArray)
ans = 1×3
3 3 1442
Now you can multiply each 3x3 matrix by a 3x1 vector using pagemtimes() in Matlab r2020b or later.
vec = [1;2;3];
z = pagemtimes(dataArray, vec);
size(z)
ans = 1×3
3 1 1442
For releases prior to Matlab r2020b, you can set up a loop
vec = [1;2;3];
z = nan(3,1,size(dataArray,3));
for i = 1:size(dataArray,3)
z(:,:,i) = dataArray(:,:,i) * vec;
end
size(z)
ans = 1×3
3 1 1442
  3 件のコメント
Adam Danz
Adam Danz 2020 年 12 月 9 日
To reduce the 3x1x1442 array to a 3x1442 matrix use,
m = reshape(z,3,[]);
or see squeeze().
Hugo Hernández Hernández
Hugo Hernández Hernández 2020 年 12 月 9 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by