# Assign a float value to a vector originally of integer type

**URL:** <https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 6, 2023, 12:10pm UTC](https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215 "2023-12-06T12:10:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [December 6, 2023, 12:10pm UTC](https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215/1 "2023-12-06T12:10:47Z")

</div>

I created a vector:

```julia
a = [1, 2, 3]

```

Then I tried to modify its value:

```julia
a[2] = 0.1

```

I got an error.

This kind of performace is ok in MATLAB.

How to make it work in Julia? I have to declare that ‘a’ is a float vector at the very beginning?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 6, 2023, 12:24pm UTC](https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215/2 "2023-12-06T12:24:40Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> How to make it work in Julia? I have to declare that ‘a’ is a float vector at the very beginning?

Yes.

```julia
a = Float64[1, 2, 3]

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2023, 12:44pm UTC](https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215/3 "2023-12-06T12:44:53Z")

</div>

Alternatively you can include a float in your vector:

```julia
julia> [1, 2, 3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

```

(you could even go so far to leave out the zero - `[1, 2, 3.]` will work but I consider these decimal-less float numbers a bit of a foot gun)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 6, 2023, 1:24pm UTC](https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215/4 "2023-12-06T13:24:13Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> This kind of performace is ok in MATLAB.

It’s OK in Matlab because `[1,2,3]` is already a double float array in Matlab

```julia
>> class(a)
ans =
    'double'

```

If you modify `a` you will get

```julia
>> a(2) = 0.1
a =
    1.0000 0.1000 3.0000

```

You have to take extra steps to make an integer array in Matlab:

```julia
>> b = int32([1,2,3])
b =
  1×3 int32 row vector
   1 2 3

```

Now, if you modify `b`, it rounds to the closest `int32`:

```julia
>> b(2) = 0.1
b =
  1×3 int32 row vector
   1 0 3

```

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [December 6, 2023, 1:24pm UTC](https://discourse.julialang.org/t/assign-a-float-value-to-a-vector-originally-of-integer-type/107215/5 "2023-12-06T13:24:51Z")

</div>

To be clear, the reason this works in Matlab is that Matlab by default stores all numbers as floating point under the hood.

In Julia, the numbers `[1, 2, 3]` are all interpreted as integers, and a vector containing only integers will be a vector of integers. You can’t insert a floating point value directly into an integer type, so you get an error.

These proposed solutions are to either specify up front that the vector should be of a floating point type, or to specify the numbers as `[1.0, 2.0, 3.0]` since those are interpreted as floating point values.
