# How to convert from bool to float

**URL:** <https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216>\
**Category:** General Usage\
**Tags:** convert, float, boolean\
**Created:** [October 24, 2022, 10:26pm UTC](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216 "2022-10-24T22:26:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xavier_gonzalez/32/36033_2.png) [@Xavier\_Gonzalez](https://discourse.julialang.org/u/Xavier_Gonzalez)\
**Post date:** [October 24, 2022, 10:26pm UTC](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216/1 "2022-10-24T22:26:17Z")

</div>

I would like to take a matrix that is currently made of bools and turn it into floats.

If `I` is this matrix of bools, I’ve been trying commands like `convert(Int8, I)` and getting errors like

```julia
ERROR: MethodError: Cannot `convert` an object of type Matrix{Bool} to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::T) where T<:Number at number.jl:6
  convert(::Type{T}, ::Number) where T<:Number at number.jl:7
  convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at twiceprecision.jl:273

```

What actually works if I want to be able to do arithmetic operations like matrix multiplications, log etc on an indicator matrix originally made from bools?

I really just want to one-hot encode a vector.  
So I’ve been doing something like

```julia
using StatsBase
v = [1 2 3]
I = indicatormat(v)

```

But then the issue is that the code I have written to run matrix decomposition on `I` says things like "There are no methods matching what you want to do, and it objects to the fact that my first entry is a `::LinearAlgebra.Transpose{Bool, Matrix{Bool}}` when it wants a `::Matrix{<:Real}`

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [October 24, 2022, 10:37pm UTC](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216/2 "2022-10-24T22:37:41Z")

</div>

You can broadcast the type constructor over the matrix:

```julia
julia> a = rand(Bool, 2, 2)
2×2 Matrix{Bool}:
 0 1
 1 0

julia> Float32.(a)
2×2 Matrix{Float32}:
 0.0 1.0
 1.0 0.0

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 24, 2022, 10:40pm UTC](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216/3 "2022-10-24T22:40:58Z")

</div>

You can convert the matrix, i.e., `convert(Matrix{Float64}, I)` or use broadcasting `convert.(Float64, I)`.  
In any case, using a dense Boolean matrix for one-hot vectors is rather wasteful. [OneHotArrays.jl](https://github.com/FluxML/OneHotArrays.jl) has a special data type which only stores the indices of the positions which are one.
