필터 지우기
필터 지우기

Element Wise Multiplication Not Giving Expected Result

조회 수: 1 (최근 30일)
Khoa Tran
Khoa Tran 2023년 4월 11일
댓글: Star Strider 2023년 4월 11일
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.

채택된 답변

Star Strider
Star Strider 2023년 4월 11일
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
Khoa Tran
Khoa Tran 2023년 4월 11일
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 2023년 4월 11일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by