Question about Julia zeros matrix?

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.

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

Maybe you want this?

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
3 Likes

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

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

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

2 Likes

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

6 Likes

Sure, you are right. My bad.

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

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

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

I then suggest reading this part of doc carefully Noteworthy Differences from other Languages · The Julia Language. And learn Julia Array syntax from the beginning. Otherwise, your question list will continue to grow.

8 Likes

Thanks for your suggetion. I will read it.

You probably want something like

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 CartesianIndexes, which are essentially (row, col) pairs. To obtain the row coordinates, you may use

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

@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.

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

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> 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> 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> [lininds[i] for i in cartinds]
3-element Vector{Int64}:
 3
 4
 1