Effacer les filtres
Effacer les filtres

Element Wise Multiplication Not Giving Expected Result

1 vue (au cours des 30 derniers jours)
Khoa Tran
Khoa Tran le 11 Avr 2023
Commenté : Star Strider le 11 Avr 2023
Hi experts,
I am new to Matlab so would appreciate all your help.
I am trying to do element wise multiplication of two 3D matrices. One matrix (A) contains random integers having a size of 10x2x1,000,000; and the other one (B) contains 0 and 1 numbers having a size of 10x2x1024. All data is uint8 integer.
As a first thought, I took each slice of 10x2 in matrix A and perform element wise multiplication with each slice of 10x2 in matrix B. However, I could not get the expected results. The code is as below:
% A slice of A
A(:,:,n) = [3, 4;
2, 5;
4, 3;
3, 4;
1, 6;
5, 2;
3, 4;
4, 3;
4, 3;
6, 1];
% A slice of B
B(:,:,m) = [1, 0;
1, 0;
0, 1;
1, 0;
1, 0;
0, 1;
1, 0;
0, 1;
0, 1;
0, 1];
% Element wise multiplication
C = A.*B;
As I run this code, I got different result than what I expect it to be:
% Result from above code:
C = [3, 0;
2, 0;
0, 3;
3, 0;
0, 6;
0, 2;
0, 4;
4, 0;
0, 3;
0, 1];
% Expected result:
C = [3, 0;
2, 0;
0, 3;
3, 0;
1, 0;
0, 2;
3, 0;
0, 3;
0, 3;
0, 1];
Please help me explain what is wrong here.
Also, I have an extra question. If I were to take each slice of matrix A and perform element wise multiplication with all the slices in matrix B at one go, and repeat this computation for all the slices in matrix A, how can I do this in an efficient way?
Thank you for your help. Looking forward to receive all your suggestions.

Réponse acceptée

Star Strider
Star Strider le 11 Avr 2023
I get the ‘Expected result’ when defining ‘n’ and ‘m’ both as 1.
Perhaps the indices are the problem?
n = 1;
m = 1;
% A slice of A
A(:,:,n) = [3, 4;
2, 5;
4, 3;
3, 4;
1, 6;
5, 2;
3, 4;
4, 3;
4, 3;
6, 1];
% A slice of B
B(:,:,m) = [1, 0;
1, 0;
0, 1;
1, 0;
1, 0;
0, 1;
1, 0;
0, 1;
0, 1;
0, 1];
% Element wise multiplication
C = A.*B
C = 10×2
3 0 2 0 0 3 3 0 1 0 0 2 3 0 0 3 0 3 0 1
.
  4 commentaires
Khoa Tran
Khoa Tran le 11 Avr 2023
Something must be wrong with my current MATLAB session. It works totally fine when I copy even a big chunk out of the original matrix, but not when I perform the computation directly with it.
Also, if I use the below code, does the elemen wise multiplication works the same and give the result as a 3D matrix having the size of that of B matrix, with each slice is the product of A(:,:,n) and B(:,:,m)?
A(:,:,n).*B
Star Strider
Star Strider le 11 Avr 2023
I do not completely understand what you are doing. As I mentioned previously, the only problems I see are the matrix indexing into the third dimension.
Perhaps there should be only one index (either one of them) so that the same ‘pages’ of the 3D matrix are always multiplied together? I do not know how your code works, much less what you want it to do, so that is simply speculation on my part.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by