Say I have a KeyedArray wrapping a dense matrix:
using AxisKeys, Random
n = 100
A = KeyedArray(rand(0:1, n, n), dim1=["a$i" for i in 1:n], dim2=["b$i" for i in 1:n])
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓ dim1 ∈ 100-element Vector{String}
→ dim2 ∈ 100-element Vector{String}
And data, 100×100 Matrix{Int64}:
("b1") ("b2") ("b3") … ("b97") ("b98") ("b99") ("b100")
("a1") 0 0 0 0 1 1 1
("a2") 1 0 0 1 0 1 0
("a3") 1 1 0 1 0 0 0
("a4") 0 1 0 1 1 0 0
("a5") 1 1 1 … 0 0 0 0
("a6") 0 1 0 0 1 1 1
⋮ ⋱ ⋮
("a94") 1 1 0 1 0 1 1
("a95") 0 0 1 0 1 1 1
("a96") 1 0 1 … 0 1 0 0
("a97") 0 0 1 0 0 1 0
("a98") 1 1 1 1 0 0 0
("a99") 0 0 0 0 1 0 0
("a100") 0 1 1 0 1 1 1
What is the easiest way to convert the underlying matrix type (say from dense to sparse) without changing axis information? My best solution so far is quite unwieldy:
using SparseArrays
A_sparse = KeyedArray(sparse(A); (dimnames(A) .=> axiskeys(A))...)
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓ dim1 ∈ 100-element Vector{String}
→ dim2 ∈ 100-element Vector{String}
And data, 100×100 SparseMatrixCSC{Int64, Int64} with 5028 stored entries:
("b1") ("b2") ("b3") … ("b97") ("b98") ("b99") ("b100")
("a1") 0 0 0 0 1 1 1
("a2") 1 0 0 1 0 1 0
("a3") 1 1 0 1 0 0 0
("a4") 0 1 0 1 1 0 0
("a5") 1 1 1 … 0 0 0 0
("a6") 0 1 0 0 1 1 1
⋮ ⋱ ⋮
("a94") 1 1 0 1 0 1 1
("a95") 0 0 1 0 1 1 1
("a96") 1 0 1 … 0 1 0 0
("a97") 0 0 1 0 0 1 0
("a98") 1 1 1 1 0 0 0
("a99") 0 0 0 0 1 0 0
("a100") 0 1 1 0 1 1 1
Of course I could define a function, but I’m thinking there must be a build-in option I’m missing.