Element Wise Multiplication Not Giving Expected Result
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
.
댓글 수: 4
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 Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!