這個例子中發生了什麼?在(非)對角矩陣中尋找非零元素的速度
查看完整矩陣,查找操作在非對角矩陣上更快。 相反,獲得稀疏表示在對角矩陣上更快(這似乎是合理的)。 稀疏矩陣上的查找操作幾乎相等。
爲了好奇,有人能告訴我這裏發生了什麼?爲什麼在整個矩陣上找到非零元素要比在對角矩陣上找到它們要快?
printf("Diagonal Mat:\n\n")
A = eye(10000);
printf("Full mat: ")
tic
find(A);
toc
printf("Building sparse representation: ")
tic
As = sparse(A);
toc
printf("Sparse mat: ")
tic
find(As);
toc
printf("\n\nNon-Diagonally flagged Mat:\n\n")
A = A | A; # This removes the "Diagonal Matrix" flag from A
printf("Full mat: ")
tic
find(A);
toc
printf("Building sparse representation: ")
tic
As = sparse(A);
toc
printf("Sparse mat: ")
tic
find(As);
toc
printf("\n\nActually Non-Diagonal Mat:\n\n")
A(:,:) = 0;
A(:,1) = 1;
printf("Full mat: ")
tic
find(A);
toc
printf("Building sparse representation: ")
tic
As = sparse(A);
toc
printf("Sparse mat: ")
tic
find(As);
toc
輸出:
Diagonal Mat:
Full mat: Elapsed time is 0.204636 seconds.
Building sparse representation: Elapsed time is 5.19753e-05 seconds.
Sparse mat: Elapsed time is 7.60555e-05 seconds.
Non-Diagonally flagged Mat:
Full mat: Elapsed time is 0.0800331 seconds.
Building sparse representation: Elapsed time is 0.0924602 seconds.
Sparse mat: Elapsed time is 7.48634e-05 seconds.
Actually Non-Diagonal Mat:
Full mat: Elapsed time is 0.0799708 seconds.
Building sparse representation: Elapsed time is 0.092248 seconds.
Sparse mat: Elapsed time is 7.70092e-05 seconds.
這已經在錯誤報告中複製,所以我將其設置爲正確的答案。 – nils