Multidimensional matrix multiplication elementwise
1 view (last 30 days)
Show older comments
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
James Tursa
on 12 Jul 2018
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 = ...?
Answers (1)
Prateekshya
on 3 Sep 2024
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.
0 Comments
See Also
Categories
Find more on Logical 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!