# Convert matrix elements from Int to Float

**URL:** <https://discourse.julialang.org/t/convert-matrix-elements-from-int-to-float/93980>\
**Category:** General Usage\
**Created:** [February 3, 2023, 9:56am UTC](https://discourse.julialang.org/t/convert-matrix-elements-from-int-to-float/93980 "2023-02-03T09:56:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmad88/32/32437_2.png) [@ahmad88](https://discourse.julialang.org/u/ahmad88)\
**Post date:** [February 3, 2023, 9:56am UTC](https://discourse.julialang.org/t/convert-matrix-elements-from-int-to-float/93980/1 "2023-02-03T09:56:17Z")

</div>

If I have the following matrix

`sparse([1, 2, 3, 4, 1, 2, 4, 1, 3, 4, 5, 1, 2, 3, 4, 5, 3, 4, 5], [1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5], [3, -1, -1, -1, -1, 2, -1, -1, 3, -1, -1, -1, -1, -1, 4, -1, -1, -1, 2], 5, 5)`

how I can convert the matrix elements to float instead of integer? something like this:

`sparse([1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 4.0, 1.,0 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0], [1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 5.0, 5.0, 5.0],[3.0, -1.0, -1.0, -1.0, -1.0, 2.0, -1.0, -1.0, 3.0, -1.0, -1.0, -1.0, -1.0, -1.0, 4.0, -1.0, -1.0, -1.0, 2.0], 5.0, 5.0)`

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 3, 2023, 9:57am UTC](https://discourse.julialang.org/t/convert-matrix-elements-from-int-to-float/93980/2 "2023-02-03T09:57:37Z")

</div>

If `A` is your array, use `float.(A)`.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [February 3, 2023, 8:06pm UTC](https://discourse.julialang.org/t/convert-matrix-elements-from-int-to-float/93980/3 "2023-02-03T20:06:57Z")

</div>

Alternatively, use the type you want in front of the values vector (3rd argument). For example, for `Float64` values use

```julia
sparse([i1,i2,...],[j1,j2,...],Float64[v1,v2,...],m,n)

```

Note that you must use an integer type for the `i` and `j` vectors, as well as the dimensions `m` and `n`. Floating point values are only acceptable in the `v` argument. If you have the arguments as vectors, then `sparse(i,j,float.(v),m,n)` will work. If you already have the matrix built, then use the above suggestion of `float.(A)` where `A` is your matrix. If you specifically want some type like `Float32` or `BigFloat`, replace `float` in the above calls with those instead.

---

<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:** [February 3, 2023, 8:19pm UTC](https://discourse.julialang.org/t/convert-matrix-elements-from-int-to-float/93980/4 "2023-02-03T20:19:21Z")

</div>

> [@jar1](#):
>
> If `A` is your array, use `float.(A)`.

You don’t need the dot; `float(A)` works too, and can be more efficient (e.g. it doesn’t make a copy if `A` is already a floating-point array, and has specialized methods for particular array types). See also the discussion in [https://github.com/JuliaLang/julia/pull/18495](https://github.com/JuliaLang/julia/pull/18495)

In particular, the original question in this thread is about sparse matrices, and `float(A)` has a [specialized method for SparseMatrixCSC](https://github.com/JuliaSparse/SparseArrays.jl/blob/8f442b8451a37da05da05fa8db32593f9b0ba7de/src/sparsematrix.jl#L973).

You can do slightly better if you are willing to share the same row and column index vectors as the original array, rather than making a copy as `float(A)` does, in which case you can do:

```julia
SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), float(nonzeros(A)))

```

(Honestly, I think that this should be the default behavior of `float(A)` for sparse matrices, since `float(A)` does not necessarily make a copy anyway: [float(S) and complex(S) should not copy data unnecessarily by stevengj · Pull Request #340 · JuliaSparse/SparseArrays.jl · GitHub](https://github.com/JuliaSparse/SparseArrays.jl/pull/340))
