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

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 Kommentare

sum(C,2)
calculates the sum of each row in resultant C matrix.
Thanks for pointing it out. I have corrected my question.
Is it possible to bypass the matrix C
What do you mean by bypass?
Don't calculate C, which is memory consuming. Is it possible to find a fast way to get the row sum of A*B?
What are the sizes of A and B?
Depending on the data, A can be more than 1e4, and B can be more than 1e6~1e7.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 4 Apr. 2018
Bearbeitet: Stephen23 am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Gefragt:

Xin
am 4 Apr. 2018

Bearbeitet:

am 4 Apr. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by