필터 지우기
필터 지우기

Array of Matrices multiplication

조회 수: 20 (최근 30일)
Hugo Hernández Hernández
Hugo Hernández Hernández 2020년 12월 9일
댓글: 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by