How to initialize an empty 1x2-dimensional row-array in Julia?

I can do this:

julia> vec=[]
0-element Array{Any,1}

julia> for i in 1:10
               global vec=vcat(vec,i)
       end

julia> vec
10-element Array{Any,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

but how can I create row vector like this? - more precisely : initialize 1x0-dimensional (row) array

julia> vec=[nothing]
1-element Array{Nothing,1}:
 nothing

julia> for i in 1:10
               global vec=hcat(vec,i)
       end

julia> vec
1×11 Array{Union{Nothing, Int64},2}:
 nothing  1  2  3  4  5  6  7  8  9  10

This doesn’t work ↓

              vec=reshape(vec,(0,1))

or this → reshape([],0,2)

julia> vec=[]
0-element Array{Any,1}

julia> vec=reshape(vec,(0,1))
0×1 Array{Any,2}

julia> for i in 1:10
               global vec=hcat(vec,i)
       end
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 0 got 1)")

There is no such thing as a “initialize 1x0-dimensional (row) array”

You are asking for a matrix with 1 row and ZERO columns.

====
Perhaps you mean a vector (or 1D array with 10 elements)

julia> [ k for k in 1:10 ]
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

Or a 1x10 matrix (or 2D array with 1x10 elements)

julia> reshape([ k for k in 1:10 ],1,10)
1×10 Array{Int64,2}:
 1  2  3  4  5  6  7  8  9  10

Or a 10x1 matrix (or 2D array with 10x1 elements)

julia> reshape([ k for k in 1:10 ],10,1)
10×1 Array{Int64,2}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

Not exactly sure what you want, but would this work for you?

Array{Int,2}(undef, 1, 0)

x = Array{Int,2}(undef, 1, 0)
for i in 1:10
    global x = hcat(x, i)
end
x
1×10 Array{Int64,2}:
 1  2  3  4  5  6  7  8  9  10
3 Likes

In terms of linear algebra you would usually produce a row vector with transposition.

julia> col_a = collect(1:4)
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> row_a = a'
1×4 Adjoint{Int64,Array{Int64,1}}:
 1  2  3  4

this can be used for a dot product which results in a scalar value:

julia> row_a*col_a
30

For real valued vectors it is common to use the ' operator to do the transpose. However on complex valued vectors you should use transpose().

julia> x = Array{Int,2}(undef, 0,1)
0×1 Array{Int64,2}

julia> for i in 1:10
       global x = vcat(x,i)
       end

julia> x
10×1 Array{Int64,2}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

I must admit, I am still very very uncomfortable with
0×1 Array{Int64,2}

It just feels wrong to me

Okay 1xN Vector (Matrix) but with no elements… like

vec=[]

So there is such thing.
Because you can’t do this :

julia> vec=[]
0-element Array{Any,1}

julia> for i in 1:10
               global vec=hcat(vec,i)
       end
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 0 got 1)")
Stacktrace:
 [1] hcat(::Array{Any,1}, ::Int64) at ./abstractarray.jl:1421
 [2] top-level scope at ./REPL[211]:2

I have never done any mathematics where I used a matrix which has no elements in the matrix.

Since it has no elements, you can use any constructor that has the right type. All of these are equivalent:

Array{Int, 2}(undef, 1, 0)
zeros(Int, 1, 0)
ones(Int, 1, 0)
fill(0, 1, 0)
fill(-4711, 1, 0)
1 Like