Convert a dense matrix A into a sparse matrix with sparsevec(A)

788 views
Skip to first unread message

Parkway

unread,
Aug 20, 2016, 7:15:34 AM8/20/16
to julia-dev
The doc for the function sparsevec(A) says: 
sparsevec(A)
Convert a dense vector A into a sparse matrix of size m x 1. In julia, sparse vectors are really just sparse matrices with one column.

What does this actually mean? For example, what does the dense vector A = [23.1, 42.67, 8.246, 111.33] get converted to?

Cameron McBride

unread,
Sep 9, 2016, 11:34:24 AM9/9/16
to Julia Dev, julia-users
This thread is old, but I was poking through some unread bits and found it. Beyond that, it’s likely a better question for julia-users list (which I’m including here).

Anyhow, the answer is that sparsevec converts a dense matrix into a sparse matrix format where zero values are not explicitly stored. If the vector (or matrix) really is dense, this does not save space.

julia> A = [23.1, 42.67, 8.246, 111.33]
4-element Array{Float64,1}:
  23.1
  42.67
   8.246
 111.33

julia> sparsevec(A)
Sparse vector of length 4 with 4 Float64 nonzero entries:
  [1]  =  23.1
  [2]  =  42.67
  [3]  =  8.246
  [4]  =  111.33


julia> B = [0,0,A...]
6-element Array{Float64,1}:
   0.0
   0.0
  23.1
  42.67
   8.246
 111.33

julia> sparsevec(B)
Sparse vector of length 6 with 4 Float64 nonzero entries:
  [3]  =  23.1
  [4]  =  42.67
  [5]  =  8.246
  [6]  =  111.33

Cameron

Reply all
Reply to author
Forward
0 new messages