i have 8 vectors a11, a12, a21, a22 and b11, b12, b21, b22 let's say of length 1x100. i want to do a*b matrix multiplication for the 2x2 matrices [a11 a12; a21 a22] and [b11 b12; b21 b22] and along the dimension of length 100. how to code this without using do loops?

 採用された回答

Matt J
Matt J 2019 年 9 月 6 日

0 投票

result=nan(2,2,100);
result(1,1,:)=a11.*b11 + a12.*b21;
result(1,2,:)=a11.*b12 + a12.*b22;
result(2,1,:)=a21.*b11 + a22.*b21;
result(2,2,:)=a21.*b12 + a22.*b22;

2 件のコメント

MURTY KOMPELLA
MURTY KOMPELLA 2019 年 9 月 6 日
Matt, thanks very much for your answer so fast! Appreciate it.
Matt J
Matt J 2019 年 9 月 6 日
You're welcome, but please Accept-click the answer if you are satisfied with it.

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

その他の回答 (2 件)

Fabio Freschi
Fabio Freschi 2019 年 9 月 6 日

0 投票

Let's start saying that the data structure you are using is not the best one. See https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
If you really want to keep that structure, maybe the simplest answer is to make the manual multiplication
C = A*B <- matrix form
c11 = a11*b11+a12*b21 <- element form
since you have array, use .* operator
c11 = a11.*b11+a12.*b21
Note that this works only for 2x2 matrices.
For a more general approach, see

4 件のコメント

MURTY KOMPELLA
MURTY KOMPELLA 2019 年 9 月 6 日
Hello Fabio, Thank you for your fast answer. I'm sure you are correct, I may not be using the best data structure. I understand your answer to do the manual multiplication.
I may be mistaken here but if i put these arrays into a and b 3-D matrices of size 2x2x100, Matlab does not implicitly do the 2-D slice matrix multiplication along the 3rd dimension.
Fabio Freschi
Fabio Freschi 2019 年 9 月 6 日
編集済み: Fabio Freschi 2019 年 9 月 6 日
You can still make the slicing manually
% data
N = 10; A = rand(2,2,N); B = rand(2,2,N);
% preallocation
C = zeros(2,2,N);
% calculation
C(1,1,:) = A(1,1,:).*B(1,1,:) + A(1,2,:).*B(2,1,:);
C(1,2,:) = A(1,1,:).*B(1,2,:) + A(1,2,:).*B(2,2,:);
C(2,1,:) = A(2,1,:).*B(1,1,:) + A(2,2,:).*B(2,1,:);
C(2,2,:) = A(2,1,:).*B(1,2,:) + A(2,2,:).*B(2,2,:);
Fabio Freschi
Fabio Freschi 2019 年 9 月 6 日
If you have data stored in cell arrays, you can use arrayfun
% data
N = 10;
A0 = arrayfun(@(i)rand(2,2),1:N,'UniformOutput',false);
B0 = arrayfun(@(i)rand(2,2),1:N,'UniformOutput',false);
% calculation
C = arrayfun(@(k)A0{k}*B0{k},1:N,'UniformOutput',false)
MURTY KOMPELLA
MURTY KOMPELLA 2019 年 9 月 6 日
Hello Fabio, now (using arrayfun) you are going above my comfort level lol! This is very interesting, let me look into it, Thanks!

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

Catalytic
Catalytic 2019 年 9 月 6 日

0 投票

If you have the parallel computing toolbox, you can do this on the GPU with
pagefun(@mtimes,A,B)
but this may only provide gains if the pages A(:,:,i) and B(:,:,i) are large matrices.

1 件のコメント

MURTY KOMPELLA
MURTY KOMPELLA 2019 年 9 月 6 日
Hello Sir, thanks for your answer. I do not have this toolbox.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by