Array of Matrices multiplication

46 views (last 30 days)
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.

Accepted Answer

Adam Danz
Adam Danz on 9 Dec 2020
Edited: Adam Danz on 9 Dec 2020
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 Comments
Adam Danz
Adam Danz on 9 Dec 2020
To reduce the 3x1x1442 array to a 3x1442 matrix use,
m = reshape(z,3,[]);
or see squeeze().

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!