Multiplication of High Dimensional Matrices
6 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Aneela
on 26 Apr 2024
Edited: Aneela
on 26 Apr 2024
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
0 Comments
Steven Lord
on 26 Apr 2024
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)
B = randi([10 20], 3, 3)
B4 = reshape(B, [1 1 3 3]) % or
B4 = permute(B, [3 4 1 2])
C = A.*B4
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)
check2 = C(:, :, 2, 3)
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!