How to quickly calculate the sum of the transpose of a sparse matrix?
Afficher commentaires plus anciens
Dear All,
I have a very big sparse matrix A. I want to obtain the sum of its transpose of the selected columns in A. Here is my code:
B = A(:,selectedCol)';
sumA = sum(B);
I am wondering if there is a faster way to do the above calculation.
Thanks a lot in advance.
Benson
1 commentaire
James Tursa
le 9 Juil 2021
What are the actual dimensions of A and selectedCol?
Réponses (2)
A=sprand(1e6,1e3,100/1e6); %Example
selectedCol=1:2:100;
tic;
B=sum( A(:,selectedCol)');
toc
tic
B=sum( A(:,selectedCol) ,2)';
toc
Depening on the size of selectedCols, it may also help to cast the operation as a matrix/vector multiplication
A=sprand(1e6,1e5,100/1e6);
selectedCol=1:10:size(A,2);
tic;
B=sum( A(:,selectedCol)');
toc
tic
B=sum( A(:,selectedCol) ,2)';
toc
z=false(size(A,2),1);
tic
x=z;
x(selectedCol)=1;
B=(A*x)';
toc
5 commentaires
Benson Gou
le 8 Juil 2021
Matt J
le 8 Juil 2021
But you must admit, my results show that it should be faster. Did you use the profiler to make sure that this operation was your bottleneck?
Benson Gou
le 8 Juil 2021
Matt J
le 9 Juil 2021
I do not know if you have a faster way to replace find.
Yes, don't use it. Use logical indexing instead.
"I do not know if you have a faster way to replace find."
Answers and comments on your previous threads related to FIND:
https://www.mathworks.com/matlabcentral/answers/866000-how-to-run-find-function-faster#answer_734225
Have you considered using logical indexing?
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!