Multidimensional matrix multiplication elementwise

For sake of simplicity, let’s just say, I have a 10 by 31 by 31 matrix. I want the 10 th pageof the 31 by 31 matrix to be multiplied with a 5139 by 31 matrix giving me a 5139 by 31 matrix as result.i want hadamard or element by element product between the square and rectangular matrix.
Please suggest.

4 Comments

How can you have an element-by-element product between the square and rectangular matrix? Can you give a specific example, say with a 2x3x3 matrix and a 5x3 matrix, showing the full inputs and desired output?
Side Note: In general, it is best to have your 2D pages as the first two dimensions, not the last two. That way the 2D pages are contiguous in memory.
So the objective is to take 31 variables and apply armairf. For 10 step ahead forecast you get 10 by 31 by 31 as the output. Now the original 31 variables have 5139 data each. So the residual matrix is 5139 by 31. I want to multiply the irf and residual. If I get 5139 rows for 31 variables I can plot the indices.
It’s important to achieve the 5139 rows for all variables. No matter how I change the dimension. Now I know 31 by 31 contains about 961 data or so. But by having that multiplied with 10 gets 9610 data. I need 5139 by 31. Which is much higher. Maybe I can try reshaping with repeat. I don’t know.
For this small example:
x = reshape(1:18,2,3,3);
y = reshape(1:15,5,3);
what would be the desired result, ? (i.e., show us what the elements of the result would be for this specific case)
result = ...?

Sign in to comment.

Answers (1)

To perform an element-wise product between a 31x31 matrix and a 5139x31 matrix, you need to ensure that the dimensions align properly for the operation. Since a direct element-wise multiplication is not feasible due to mismatched dimensions, you need to adjust the matrices such that each element of the 31x31 matrix is applied to the corresponding columns of the 5139x31 matrix.
One way to achieve this is to replicate the 31x31 matrix along the first dimension to create a 5139x31x31 matrix which matches the dimensions of the 5139x31 matrix. Here is the code for the same:
% Example matrices
A = rand(31, 31); % 31x31 matrix (10th page of your 3D matrix)
B = rand(5139, 31); % 5139x31 matrix
% Replicate and reshape the 31x31 matrix to match the dimensions of the 5139x31 matrix
% This creates a 5139x31x31 matrix
A_rep = repmat(A, [5139, 1, 1]);
A_rep = reshape(A_rep, [5139,31,31]);
% Reshape B to a 5139x31x1 matrix for broadcasting
B_rep = reshape(B, [5139, 31, 1]);
% Perform element-wise multiplication
% The result is a 5139x31x31 matrix
C = A_rep .* B_rep;
% If you want a 5139x31 matrix as the final result, you might need to
% sum or average across the third dimension, depending on your specific needs.
% For example, summing across the third dimension:
result = sum(C, 3);
% Display the size of the result
disp(size(result)); % Should be [5139, 31]
This approach assumes that you want to apply each element of the 31x31 matrix across the corresponding columns of the 5139x31 matrix. You can adjust the final step according to your specific requirements for combining the results.
Thank you.

Categories

Products

Release

R2017b

Asked:

on 11 Jul 2018

Answered:

on 3 Sep 2024

Community Treasure Hunt

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

Start Hunting!