Element Wise Multiplication Not Giving Expected Result

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

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
Modifié(e) : Khoa Tran le 11 Avr 2023
Yeah you are probably right. The indices and how MATLAB understands it from a big matrix is could the problem here.
Based on your information, I tried to copy each slice from the big matrix out into a new variable and it works perfectly fine (with the same index I have been using for the test). Do you have any thoughts about this?
I don’t know, other than the indices might not have been what you thought them to be. All I have iss the code you posted.
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
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

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by