Multidimensional matrix multiplication

버전 1.2.0.0 (1.75 KB) 작성자: Sandor Toth
The simple function performs fast matrix multiplication within multidimensional arrays.
다운로드 수: 1.4K
업데이트 날짜: 2013/8/19

라이선스 보기

mmat(A,B) performs matrix multiplication, where the 2D matrices are part of multidimensional arrays. It is equivalent to the Matlab built in mtimes function for 2D arrays. However it naturally extends the mtimes function, where the two input arrays can have arbitrary number of extra dimensions. For example:

A = [1 2;2 1];
B = [3 4; 1 2];

mmat(A,B) == mtimes(A,B)

However A and B can be expanded along the 3rd dimension:

A = repmat([1 2; 2 1],[1 1 5]);

C = mmat(A,B) can be also performed, C will contains:
C(:,:,1) = A(:,:,1)*B;
C(:,:,2) = A(:,:,2)*B; ...

In the above example B was expanded along the singleton dimensions to match the size of A for the multiplication.

In the above examples the matrix multiplication was performed along the first two dimensions of A and B. However when called:
mmat(A,B,dim)
then dim selects two dimensions from A and B along which the matrix multiplication should be performed.

For example:
dim = [1 2] - default value
dim = [2 3] - C(ii,:,:) = A(ii,:,:) * B(ii,:,:);

This function contains only simple Matlab script without for loops!
Thus it is clean, fast and no compiling needed!

Please leave a comment if you like/dislike it, or you found a bug!
Enjoy!

인용 양식

Sandor Toth (2024). Multidimensional matrix multiplication (https://www.mathworks.com/matlabcentral/fileexchange/41663-multidimensional-matrix-multiplication), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2012b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.2.0.0

Line 54/55 are changed, to fix a bug that appears when dim is not the default [1 2] value.

1.1.0.0

Corrected a small bug in the code and refreshed the file description.

1.0.0.0