I created a vector:

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

Then I tried to modify its value:

```
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?

I created a vector:

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

Then I tried to modify its value:

```
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?

Yes.

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

1 Like

Alternatively you can include a float in your vector:

```
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)

1 Like

It’s OK in Matlab because `[1,2,3]`

is already a double float array in Matlab

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

If you modify `a`

you will get

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

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

```
>> 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`

:

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

4 Likes

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.

3 Likes