Elementwise multiplication of two 3D matrices

7 vues (au cours des 30 derniers jours)
Alexander
Alexander le 10 Fév 2025
Réponse apportée : Umar le 10 Fév 2025
I am trying to multiply two 3D matrices elementwise so the value in slot (1,1,1) in both matrices are multiplied together and the product taking that slot in the product matrix. I have tried to use pagemtimes to do this, but I recieved an error in response:
Error using pagemtimes
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first array matches the number of rows in the second array.
Both matrices are 100x130x100 so perhaps I am using the wrong function for what I wish to do. Would a for loop being the best direction? I am not much of a coder so apologizes if the solution seems obvious. The 3rd dimension to the matrices I think is throwing me off.

Réponse acceptée

Umar
Umar le 10 Fév 2025

Hi @Alexander,

If you review @Torsten and @Walter Roberton’s comments, they have provided some good suggestions towards to your comments. To multiply two 3D matrices element-wise in MATLAB, the appropriate operator is the dot multiplication operator `.*`, not `pagemtimes`. The `pagemtimes` function is designed for matrix multiplication, which requires that the inner dimensions match (i.e., the number of columns in the first matrix must equal the number of rows in the second). Since you want to perform element-wise operations where corresponding elements from both matrices are multiplied together, using `.*` is straightforward and efficient. Here’s how you can do it:

% Assume A and B are your two 3D matrices of size 100x130x100
A = rand(100, 130, 100); % Example initialization
B = rand(100, 130, 100); % Example initialization
% Perform element-wise multiplication
C = A .* B;
% Now C will also be a 3D matrix of size 100x130x100 containing the products

Please see attached.

In this example: - The `rand` function initializes two random matrices for demonstration purposes. - The operation `A .* B` performs element-wise multiplication across all corresponding elements in matrices A and B.

Using `.*` is generally more efficient than using a for loop, especially with large matrices. MATLAB is optimized for vectorized operations, which means that they run faster than explicit loops. Also, bear in mind when dealing with multidimensional arrays, ensure that you are clear about how each dimension corresponds to your data. Each element in a 3D matrix can be accessed using three indices (e.g., `A(i,j,k)`), where `i`, `j`, and `k` are the indices along the first, second, and third dimensions, respectively.

If you encounter issues with dimensions again, you can check the size of your matrices using the `size` function:

      size(A) % This will return [100, 130, 100]

By following this approach, you should be able to achieve your goal without running into dimensionality issues.

Don't hesitate to reach out if you have more questions or need further clarification!

Plus de réponses (2)

Torsten
Torsten le 10 Fév 2025
Déplacé(e) : Torsten le 10 Fév 2025
A = rand(3,6,9);
B = rand(3,6,9);
Product = A.*B
Product =
Product(:,:,1) = 0.2097 0.2670 0.7189 0.3690 0.0057 0.4654 0.2635 0.0964 0.5350 0.0168 0.0615 0.0498 0.3044 0.1764 0.2163 0.1997 0.0002 0.0810 Product(:,:,2) = 0.0093 0.0623 0.4904 0.0355 0.2245 0.0966 0.4260 0.1504 0.2193 0.1941 0.1518 0.6343 0.0178 0.0189 0.1973 0.0167 0.6507 0.0372 Product(:,:,3) = 0.5328 0.0589 0.0963 0.0015 0.0436 0.5033 0.6153 0.0869 0.4370 0.0216 0.1276 0.1054 0.1456 0.1212 0.1335 0.0499 0.0771 0.7093 Product(:,:,4) = 0.2083 0.2113 0.1994 0.5443 0.1525 0.7634 0.0972 0.0136 0.5825 0.5120 0.1475 0.0409 0.2820 0.1992 0.0096 0.6186 0.2334 0.0250 Product(:,:,5) = 0.3990 0.7738 0.0358 0.0179 0.1066 0.2396 0.0463 0.2489 0.0610 0.1646 0.4939 0.4164 0.2429 0.3995 0.5649 0.1287 0.0357 0.3702 Product(:,:,6) = 0.7966 0.0261 0.3149 0.2750 0.0970 0.7108 0.3545 0.0759 0.3576 0.2803 0.0109 0.0391 0.2349 0.4150 0.4819 0.5329 0.1140 0.2128 Product(:,:,7) = 0.0143 0.3268 0.1145 0.1358 0.0021 0.1630 0.1034 0.2588 0.5594 0.9224 0.0222 0.4380 0.0449 0.0078 0.1940 0.0052 0.7915 0.1940 Product(:,:,8) = 0.3673 0.6390 0.1302 0.1298 0.0807 0.5834 0.4420 0.0065 0.1088 0.1664 0.6933 0.6158 0.4850 0.2838 0.2993 0.1041 0.0038 0.0623 Product(:,:,9) = 0.0122 0.0555 0.2196 0.4697 0.1452 0.1456 0.0216 0.4819 0.0241 0.6925 0.1244 0.1924 0.3586 0.3604 0.4747 0.2707 0.4369 0.3038

Walter Roberson
Walter Roberson le 10 Fév 2025
pagemtimes is for page-by-page algebraic matrix multiplication. It takes two matrices of size [A B C] and [B D C] and produces a result that is size [A D C]. It is not element-by-element matrix multiplication. Element-by-element matrix multiplication is just the .* operator

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by