Multiplication of High Dimensional Matrices

5 ビュー (過去 30 日間)
Meng Li
Meng Li 2021 年 8 月 24 日
回答済み: Steven Lord 2024 年 4 月 26 日
Hello everyone!
I have a 4-D matrix and a 2-D matrix. I want to multiply the two and form a new 6-D matrix. Please see the following example.
corr_tauk=1:4;
corr_taun=1:4;
corr_tauc=1:4;
corr_g=1:4;
periods=2500;
vnodes_e=2000;
RtnDtr_e=randn(periods,vnodes_e);
r_itr=ones(length(corr_tauk),length(corr_taun),length(corr_tauc),length(corr_g));
Mtx_ridio=zeros(length(corr_tauk),length(corr_taun),length(corr_tauc),length(corr_g),periods,vnodes_e);
Now I confront a big problem to get Mtx_ridio. The idea is pointwise-multiplication to form an even higher dimensional matrix to reflect the cross-section at each period with each mix of parameters.
It seems like Mtx_ridio=r_itr.*RtnDtr_e, but definitely not.
Does anyone have an easy solution?

回答 (2 件)

Aneela
Aneela 2024 年 4 月 26 日
編集済み: Aneela 2024 年 4 月 26 日
Hi Meng Li,
The point-wise multiplication you mentioned:
Mt_ridio=r_itr.*RtnDtr_e
This throws an error as “r_itr” and “RtnDtr_e” are having incompatible sizes.
I tried the below example in MATLAB, and it worked. I reduced the values of “periods” and “vnodes_e” to 10 each, to reduce the compilation time.
corr_tauk=1:4;
corr_taun=1:4;
corr_tauc=1:4;
corr_g=1:4;
periods=10;
vnodes_e=10;
RtnDtr_e=randn(periods,vnodes_e);
r_itr=ones(length(corr_tauk),length(corr_taun),length(corr_tauc),length(corr_g));
Mtx_ridio=zeros(length(corr_tauk),length(corr_taun),length(corr_tauc),length(corr_g),periods,vnodes_e);
for i = 1:length(corr_tauk)
for j = 1:length(corr_taun)
for k = 1:length(corr_tauc)
for l = 1:length(corr_g)
Mtx_ridio(i,j,k,l,:,:) = r_itr(i,j,k,l) .* RtnDtr_e;
end
end
end
end
For more information on point-wise multiplication, refer to the following MathWorks Documentation: https://www.mathworks.com/help/matlab/ref/times.html

Steven Lord
Steven Lord 2024 年 4 月 26 日
Either reshape or permute your matrix into a 6-dimensional array whose first four dimensions are singletons. Then use times. Here's a smaller example that creates a 4-dimensional array from two 2-dimensional matrices.
A = magic(3)
A = 3x3
8 1 6 3 5 7 4 9 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = randi([10 20], 3, 3)
B = 3x3
15 10 12 18 17 16 18 17 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B4 = reshape(B, [1 1 3 3]) % or
B4 =
B4(:,:,1,1) = 15 B4(:,:,2,1) = 18 B4(:,:,3,1) = 18 B4(:,:,1,2) = 10 B4(:,:,2,2) = 17 B4(:,:,3,2) = 17 B4(:,:,1,3) = 12 B4(:,:,2,3) = 16 B4(:,:,3,3) = 20
B4 = permute(B, [3 4 1 2])
B4 =
B4(:,:,1,1) = 15 B4(:,:,2,1) = 18 B4(:,:,3,1) = 18 B4(:,:,1,2) = 10 B4(:,:,2,2) = 17 B4(:,:,3,2) = 17 B4(:,:,1,3) = 12 B4(:,:,2,3) = 16 B4(:,:,3,3) = 20
C = A.*B4
C =
C(:,:,1,1) = 120 15 90 45 75 105 60 135 30 C(:,:,2,1) = 144 18 108 54 90 126 72 162 36 C(:,:,3,1) = 144 18 108 54 90 126 72 162 36 C(:,:,1,2) = 80 10 60 30 50 70 40 90 20 C(:,:,2,2) = 136 17 102 51 85 119 68 153 34 C(:,:,3,2) = 136 17 102 51 85 119 68 153 34 C(:,:,1,3) = 96 12 72 36 60 84 48 108 24 C(:,:,2,3) = 128 16 96 48 80 112 64 144 32 C(:,:,3,3) = 160 20 120 60 100 140 80 180 40
To check let's multiply A by one of the elements in B then compare it against the appropriate section of C.
check1 = A.*B(2, 3)
check1 = 3x3
128 16 96 48 80 112 64 144 32
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
check2 = C(:, :, 2, 3)
check2 = 3x3
128 16 96 48 80 112 64 144 32
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by