# Refactoring a symmetric matrix into a vector and back again

**URL:** <https://discourse.julialang.org/t/refactoring-a-symmetric-matrix-into-a-vector-and-back-again/105378>\
**Category:** General Usage\
**Created:** [October 25, 2023, 2:18pm UTC](https://discourse.julialang.org/t/refactoring-a-symmetric-matrix-into-a-vector-and-back-again/105378 "2023-10-25T14:18:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ignace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ignace/32/24275_2.png) [@ignace](https://discourse.julialang.org/u/ignace)\
**Post date:** [October 25, 2023, 2:18pm UTC](https://discourse.julialang.org/t/refactoring-a-symmetric-matrix-into-a-vector-and-back-again/105378/1 "2023-10-25T14:18:56Z")

</div>

Hallo,

Do you know ‘good’ ways to refactor a symmetric matrix into a vector, and back again?  
Ideally, something like this:

> A = [1 2 3; 2 4 5; 3 5 6]  
> V = mat2vec(A) # V = [1 2 3 4 5 6]  
> A = vec2mat(V) # A = [1 2 3; 2 4 5; 3 5 6]

Many thanks,

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 25, 2023, 2:37pm UTC](https://discourse.julialang.org/t/refactoring-a-symmetric-matrix-into-a-vector-and-back-again/105378/2 "2023-10-25T14:37:01Z")

</div>

> [@ignace](#):
>
> Do you know ‘good’ ways to refactor a symmetric matrix into a vector,

See

- [Vector of upper triangle](https://discourse.julialang.org/t/vector-of-upper-triangle/7764)
- [GitHub - JaneliaSciComp/SymmetricFormats.jl: efficient storage for symmetric matrices](https://github.com/JaneliaSciComp/SymmetricFormats.jl)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 26, 2023, 7:06am UTC](https://discourse.julialang.org/t/refactoring-a-symmetric-matrix-into-a-vector-and-back-again/105378/3 "2023-10-26T07:06:23Z")

</div>

Maybe it’s not a “good” way in the sense you expect, but in some cases it could work

```julia
julia> function symmat2vec(m)
           n=size(m,1)
           N=n*(n+1)÷2
           v=Vector{eltype(m)}(undef,N)
               for idx in 1: N
               j = Int(ceil(sqrt(2 * idx + 0.25) - 0.5))
               i = Int(idx - (j-1) * (j) / 2)
               v[idx] = m[i, j]
           end
           v
       end
symmat2vec (generic function with 1 method)

julia> function vec2symat(v)
           N=size(v,1)[1]
           n=Int((-1+ sqrt(1+8*N))/2)
           m=Matrix{eltype(v)}(undef,n,n)
           rngs=[Int(n*(n-1)/2+1):Int(n*(n+1)/2) for n in 1:4]
           for c in 1:4
               r=4*(c-1)+1:4*(c-1)+4
               m[r]= v[[rngs[c];getindex.(rngs[c+1:end],c)]]
           end
           m
       end
vec2symat (generic function with 1 method)

julia> m=[11 2 13 4; 2 3 4 25; 13 4 5 6; 4 25 6 77]  
4×4 Matrix{Int64}:
 11 2 13 4
  2 3 4 25
 13 4 5 6
  4 25 6 77

julia> v=symmat2vec(m)
10-element Vector{Int64}:
 11
  2
  3
 13
  4
  5
  4
 25
  6
 77

julia> vec2symat(v)
4×4 Matrix{Int64}:
 11 2 13 4
  2 3 4 25
 13 4 5 6
  4 25 6 77

```
