这个例子中发生了什么?在(非)对角矩阵中寻找非零元素的速度
查看完整矩阵,查找操作在非对角矩阵上更快。 相反,获得稀疏表示在对角矩阵上更快(这似乎是合理的)。 稀疏矩阵上的查找操作几乎相等。
为了好奇,有人能告诉我这里发生了什么?为什么在整个矩阵上找到非零元素要比在对角矩阵上找到它们要快?
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