MATLAB Answers

Column vector multiplied to a row vector, memory v.s. speed issue

조회 수: 6(최근 30일)
Hello everyone. I have a column vector (A) that is left multiplied to a row vector (B). The result is a matrix C=A*B. What I need is the sum of C in each row, that is sum(C,2). However, both A and B are very long vector that makes C rather huge that exceeds memory. Is it possible to bypass the matrix C and get the results sum(C,2) without using a loop, which is a bit slow.
Many thanks

  댓글 수: 6

표시 이전 댓글 수: 3
Xin
Xin 4 Apr 2018
Don't calculate C, which is memory consuming. Is it possible to find a fast way to get the row sum of A*B?
Birdman
Birdman 4 Apr 2018
What are the sizes of A and B?
Xin
Xin 4 Apr 2018
Depending on the data, A can be more than 1e4, and B can be more than 1e6~1e7.

로그인 to comment.

채택된 답변

Stephen Cobeldick
Stephen Cobeldick 4 Apr 2018
Stephen Cobeldick 님이 편집함. 4 Apr 2018
Loop over A, it won't take very long as long as long as the output is preallocated. Replace this:
D1 = sum(A*B,2)
with
N = numel(A);
D2 = nan(N,1);
for k = 1:N
D2(k) = sum(A(k)*B);
end

  댓글 수: 0

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by