Output sparse matrix to csv

Ok, could you try this:

using SparseArrays, DelimitedFiles
M = sprand(5, 5, 0.5)
Md = Matrix(M)          # creates dense Matrix{Float64}
Mb = similar(Md,Any)    # similar Matrix{Any}, type Any allows mixing floats with strings
Mb .= Md                # assigns content of Md to Mb
Mb[Md .== 0] .= ""      # bitmatrix Md.==0 provides indices of all 0's
open("spmatrix_blanks.csv", "w") do io
    writedlm(io, Mb, ',')
end
1 Like