Not able to understand why everything is Vector or Matrix types which are alias types of Array

How we should create an object whose type is Array not Vector or Matrix which are aliases of Arrays

For example if I can create an Array

x = ["random", 1, "test", 3.1455]
y = ["Hello" 1 ; "test" "random"]

then typeof output for

typeof(x)
typeof(y)

Results in
Vector{Any} (alias for Array{Any, 1})
Matrix{Any} (alias for Array{Any, 2})

What is the problem with one- and two-dimensional Array types having shorter equivalent names?

Anyway, if you absolutely want to create an object of an Array type which doesn’t happen to have a shorter alias, you can do

julia> z = ["Hello" ;;; 1]
1×1×2 Array{Any, 3}:
[:, :, 1] =
 "Hello"

[:, :, 2] =
 1

julia> typeof(z)
Array{Any, 3}
1 Like

Thanks, this is slightly confusing when coming from Python, I am checking when we have Vector, Matrices and Arrays

They are all the same thing. Vector is a synonym for a 1d Array and Matrix is a synonym for a 2d Array. The short names are just to make it easier to type and talk about.

7 Likes

Just to make this abundantly clear, Vector{T} is a type alias (see the docs here and here) for Array{T,1}, and Matrix{T} is a type alias for Array{T,2}. Type aliases are just new names for types that are exactly equivalent to the old names:

julia> Vector{Int} === Array{Int,1}
true

julia> Matrix{Int} === Array{Int,2}
true

You can even define your own type aliases using regular assignment (usually to a const variable):

julia> struct LongName{S,T} end

julia> const LN = LongName{S,T} where {S,T};

julia> LN === LongName
true

julia> const LN2{S} = LongName{S,Int} where S
LN2 (alias for LongName{S, Int64} where S)

julia> LN2{String} === LongName{String, Int}
true
1 Like

Array (e.g. Array{Any, 2}) is the general name for a collection of some type (e.g. Any) across some number of dimensions (e.g. 2). The most common number of dimensions are 1-dimensional arrays (also called vectors) and 2-dimensional arrays (also called matrices). We just don’t give special names to any of the arrays with dimensions larger than 2.

x = ["random", 1, "test", 3.1455]  # 4-element Vector{Any}
y = ["Hello" 1 ; "test" "random"]  # 2×2 Matrix{Any}
x isa Array   # true
y isa Array   # true
x isa Vector  # true
y isa Vector  # false
x isa Matrix  # false
y isa Matrix  # true
Vector{Any} === Array{Any, 1}  # true
Matrix{Any} === Array{Any, 2}  # true
2 Likes