mxn and nxn matrix element-wise multiplication without for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
I have 2 2D matrices. First one, A, is mxn where m=n*k and second one, B, is nxn. How do I do element-wise multiplication of every nxn square sub matrix of A with B without for loop? and sum all elements of every multiplied nxn matrix and get a nx1 vector? Below is what I did with for loop. I need to perform the for loop in one row.
n = 10;
k = 5;
A = rand(k*n,n);
B = rand(n,n);
results = zeros(k,1);
for i=1:k
results(i) = sum(B.*A(1+n*(i-1):i*n,:),'all');
end
댓글 수: 6
Cris LaPierre
2020년 10월 26일
편집: Cris LaPierre
2020년 10월 26일
What about the requirement to use elementwise multiplication and not use for loops? What about achieving this all in a single row?
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!