필터 지우기
필터 지우기

Is it faster to operate a sparse matrix than a dense one when they have the same size?

조회 수: 17 (최근 30일)
Hi all,
Imagine I have 2 same size matrices, both takes only the upper triangular part and sparse, their sparsity is like this:
a:
b:
I'm being a bit exaggerate here, in reality a would be more dense. Of course a takes much less memory than b.
If I do some operations on a and b, for example, a.*c and b.*c, where matrix c has same size, but dense, will a.*c faster than b.*c?

답변 (2개)

Star Strider
Star Strider 2017년 7월 29일
편집: Star Strider 2017년 7월 29일
The best way to find out is to experiment! This is especially true if you have a specific example, such as you illustrated in your Question.
Use the timeit (link) function with two functions, one with a sparse matrix and one with a dense matrix. Compute the results you want to test with various sparse matrix and dense matrix functions and see how they compare.
EDIT
For an extended discussion of sparse matrix computations, see the documentation on Sparse Matrix Operations (link).

Matt J
Matt J 2017년 7월 29일
a.*c should be faster. Since a is sparser than b, there are fewer multiplication operations that need executing. The question is, how much faster. That depends on how MATLAB parallelizes the operation and could be hard to predict. If you are working on the GPU, I suspect that the performance difference may be a lot less than on the CPU.
  댓글 수: 2
Xh Du
Xh Du 2017년 7월 29일
If the sparse matrix is large but very sparse, like this 1501 by 1501 matrix "exam_a":
and I have some operations like
exam = exam_a .* mtx_b
obviously in 'exam', wherever is zero in 'exam_a' will also be zero in 'exam'. But does MATLAB know this such that it only multiplies the non-zero part, or MATLAB doesn't know and multiplies everything?
If MATLAB does not know and multiplies everything, maybe I should extract these non-zero part and operate on them only.
Matt J
Matt J 2017년 7월 29일
편집: Matt J 2017년 7월 29일
Yes, MATLAB knows. If you do the extraction you describe, you will be duplicating what MATLAB already does.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by