Given that A is a sparse matrix, norm(A(i,:)) takes a very long time. Why and can one do better ?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cem Gormezano
am 9 Aug. 2020
Bearbeitet: Walter Roberson
am 11 Aug. 2020
I am computing norm(A(i,:)) within a loop, where A is a sparse matrix. This seems to be a bottleneck, is there anyway to perform this faster ? Thank you.
1 Kommentar
Walter Roberson
am 10 Aug. 2020
Can you recode with the matrix transposed? Accessing sparse by columns is faster than by row.
Akzeptierte Antwort
John D'Errico
am 10 Aug. 2020
You really don't want to use loops to do something like this.
>> A = sprand(10000,10000,0.001);
>> timeit(@() norm(A(1,:)))
ans =
0.000121650132
>> timeit(@() sqrt(sum(A.^2,2)))
ans =
0.000442972132
So, the time to compute the norm of just one of the rows was a significant fraction of the total time to compute the norm of all rows at once.
0 Kommentare
Weitere Antworten (2)
Walter Roberson
am 11 Aug. 2020
Bearbeitet: Walter Roberson
am 11 Aug. 2020
If you were going to do this repeatedly, then
%one time cost
At = A.';
%then
norm(At(:,1))
is over 100 times faster than
norm(A(1,:))
For John's suggestion of A = sprand(10000,10000,0.001); then the cost of transposing A is roughly the same as the cost of calculating 11 row norms of A.
... And of course, it might happen to be practical to work with the transposed version instead of the original, so in some cases there would be no computation cost, just the cost of changing the code.
0 Kommentare
James Tursa
am 9 Aug. 2020
Bearbeitet: James Tursa
am 9 Aug. 2020
Will you eventually need all of the rows? E.g., do this once at the beginning outside the loop
n = sqrt(sum(A.^2,2))
And then just pick off the element you want each iteration.
Alternatively, you could do this calculation one row at a time in a mex routine in order to avoid the data copy associated with forming the A(i,:) row explicitly.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!