# Question about Julia zeros matrix?

**URL:** https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350
**Category:** General Usage
**Created:** [May 20, 2022, 6:39am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350 "2022-05-20T06:39:45Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)
#### Post date: [May 20, 2022, 6:39am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/1 "2022-05-20T06:39:45Z")

</div>

Dear all,

In matlab, we can add complex values into zeros matrix, but in Julia I cannot .Can you help me to modify my codes to add complex values into zeros matrix in Julia?

This is the matlab code.

```julia
>> A = zeros(2, 2)

A =

     0 0
     0 0

>> A(1, 1) = 1 + i

A =

   1.0000 + 1.0000i 0.0000 + 0.0000i
   0.0000 + 0.0000i 0.0000 + 0.0000i

```

And this the Julia codes, but it get an error.

```julia
julia> A = zeros(3, 3)
3×3 Matrix{Float64}:
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

julia> A[1, 1] = 1 + im
ERROR: InexactError: Float64(1 + 1im)
Stacktrace:
 [1] Real
   @ ./complex.jl:44 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] setindex!(::Matrix{Float64}, ::Complex{Int64}, ::Int64, ::Int64)
   @ Base ./array.jl:905
 [4] top-level scope
   @ REPL[24]:1

```

Any help will be appreciated.

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 20, 2022, 6:46am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/2 "2022-05-20T06:46:08Z")

</div>

Maybe you want this?

```julia
julia> A = zeros(Complex, 3, 3);
julia> A[1,1] = 1 + im
3×3 Matrix{Complex}:
 1+1im 0+0im 0+0im
 0+0im 0+0im 0+0im
 0+0im 0+0im 0+0im

```

---

<div class="post-metadata">

### Author: ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)
#### Post date: [May 20, 2022, 6:48am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/3 "2022-05-20T06:48:54Z")

</div>

I try to modify the Julia code like below, is it right?

```julia
julia> A = convert(Array{Any}, zeros(2, 2))
2×2 Matrix{Any}:
 0.0 0.0
 0.0 0.0

julia> A[1, 1] = 1 + im
1 + 1im

julia> A
2×2 Matrix{Any}:
 1+1im 0.0
  0.0 0.0

```

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 20, 2022, 6:50am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/4 "2022-05-20T06:50:54Z")

</div>

The matrix contains elements of mixed types. It generally will deteriorate your performance.

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [May 20, 2022, 7:00am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/5 "2022-05-20T07:00:44Z")

</div>

> [@liuyxpp](#):
>
> `julia> A = zeros(Complex, 3, 3);`

Since `Complex` is not a concrete type, this will also give you bad performance. Use `zeros(Complex{Float64}, 3, 3)` instead.

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 20, 2022, 7:02am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/6 "2022-05-20T07:02:34Z")

</div>

Sure, you are right. My bad.

---

<div class="post-metadata">

### Author: ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)
#### Post date: [May 20, 2022, 7:12am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/7 "2022-05-20T07:12:06Z")

</div>

@liuyxpp @sostock Thanks so much. It solves my problem.

---

<div class="post-metadata">

### Author: ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)
#### Post date: [May 20, 2022, 7:25am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/8 "2022-05-20T07:25:58Z")

</div>

@liuyxpp @sostock I have another question. How can I transform this matlab code into Julia.

```matlab
>> val = zeros(3, 3);
>> row = zeros(3, 3);
>> A = [1 2 3; 6 5 1; 1 7 4]

A =

     1 2 3
     6 5 1
     1 7 4

[val(1, :), row(1, :)] = max(A)

val =

     6 7 4
     0 0 0
     0 0 0

row =

     2 3 3
     0 0 0
     0 0 0

```

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 20, 2022, 7:48am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/9 "2022-05-20T07:48:33Z")

</div>

I then suggest reading this part of doc carefully [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB). And learn Julia Array syntax from the beginning. Otherwise, your question list will continue to grow.

---

<div class="post-metadata">

### Author: ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)
#### Post date: [May 20, 2022, 8:01am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/10 "2022-05-20T08:01:59Z")

</div>

Thanks for your suggetion. I will read it.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 20, 2022, 8:46am UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/11 "2022-05-20T08:46:32Z")

</div>

You probably want something like

```julia
julia> A
3×3 Matrix{Int64}:
 1 2 3
 6 5 1
 1 7 4

julia> vals, inds = findmax(A, dims=1)
([6 7 4], CartesianIndex{2}[CartesianIndex(2, 1) CartesianIndex(3, 2) CartesianIndex(3, 3)])

```

The indices are `CartesianIndex`es, which are essentially `(row, col)` pairs. To obtain the row coordinates, you may use

```julia
julia> first.(Tuple.(inds))
1×3 Matrix{Int64}:
 2 3 3

```

---

<div class="post-metadata">

### Author: ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)
#### Post date: [May 21, 2022, 8:31pm UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/12 "2022-05-21T20:31:22Z")

</div>

@jishnub  
Thanks for your help. I have another problem .How can I use `LinearIndices` to get the index. In matlab, we can use `sub2ind` .The matlab code is below.

```julia
>> A = [1 2 1; 2 2 1]

A =

     1 2 1
     2 2 1

>> inds = sub2ind([2,2], A(1,:), A(2,:))

inds =

     3 4 1

```

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 22, 2022, 3:06pm UTC](https://discourse.julialang.org/t/question-about-julia-zeros-matrix/81350/13 "2022-05-22T15:06:58Z")

</div>

The way to do this is to create a list of indices as `CartesianIndex(rowind, colind)` pairs. In this case, we may broadcast `CartesianIndices` over a list of row and column indices (Tuples, instead of creating a matrix as in Matlab).

```julia
julia> cartinds = CartesianIndex.((1,2,1), (2,2,1))
(CartesianIndex(1, 2), CartesianIndex(2, 2), CartesianIndex(1, 1)

```

After this, we create a list of all linear indices of a 2x2 matrix:

```julia
julia> lininds = LinearIndices((2,2))
2×2 LinearIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}:
 1 3
 2 4

```

Finally, we index into `lininds` at the indices that we want.

```julia
julia> [lininds[i] for i in cartinds]
3-element Vector{Int64}:
 3
 4
 1

```
