Array{T,N}
8 methodsA dense N-dimensional array of element type T. Backed by contiguous memory in column-major order; passed by reference and mutated in place with bang-suffixed functions.
zeros([T=Float64], dims...) -> Array{T,N}Create an N-dimensional array filled with zero(T), with the given shape.
Parameters
| Name | Type | Description |
|---|---|---|
| T | Type | Element type (defaults to Float64). |
| dims | Int... | Dimensions of the array; number of dims sets N. |
Returns
Array{T,N} filled with zeros.
Example
zeros(3) # 3-element Vector{Float64} of 0.0
zeros(Int, 2, 4) # 2x4 Matrix{Int} of 0
zeros(Float32, 2, 2, 2) # 2x2x2 Array{Float32,3}ones([T=Float64], dims...) -> Array{T,N}Create an N-dimensional array filled with one(T).
Parameters
| Name | Type | Description |
|---|---|---|
| T | Type | Element type (defaults to Float64). |
| dims | Int... | Dimensions of the array. |
Returns
Array{T,N} filled with ones.
Example
ones(3) # [1.0, 1.0, 1.0]
ones(Int, 2, 2) # 2x2 matrix of 1
3.0 .* ones(5) # vector of 3.0 (broadcast)reshape(A, dims...) -> AbstractArrayReturn a view of A with the same data but a different shape. The total length must match.
Parameters
| Name | Type | Description |
|---|---|---|
| A | AbstractArray | Source array (data is shared, not copied). |
| dims | Int... or Tuple | New dimensions; product must equal length(A). |
Returns
A reshaped view sharing memory with A.
Example
v = 1:12
M = reshape(v, 3, 4) # 3x4 view into 1:12
reshape(M, 2, 6) # 2x6 view of the same data
reshape(1:6, 2, :) # colon infers last dim = 3transpose(A) -> AbstractArrayReturn the transpose of a 2-D matrix (rows <-> columns). For complex matrices use adjoint for conjugate transpose.
Parameters
| Name | Type | Description |
|---|---|---|
| A | AbstractMatrix | Input matrix. |
Returns
A lazy transpose view; use copy(transpose(A)) for a materialized copy.
Example
A = [1 2 3; 4 5 6] # 2x3
transpose(A) # 3x2 view:
# [1 4; 2 5; 3 6]
adjoint(A) # conjugate transpose (same for reals)
A' # shorthand for adjoint(A)cat(A...; dims) -> ArrayConcatenate arrays along dimension dims. vcat, hcat, and hvcat are common shortcuts.
Parameters
| Name | Type | Description |
|---|---|---|
| A... | AbstractArray... | Arrays to concatenate; shapes must agree on non-cat dims. |
| dims | Int or Tuple | Dimension(s) along which to concatenate. |
Returns
A new array containing the concatenated inputs.
Example
vcat([1,2], [3,4]) # [1,2,3,4]
hcat([1 2], [3 4]) # [1 2 3 4]
cat([1 2; 3 4], [5 6; 7 8], dims=2) # horizontal stack
hvcat((2,2), 1,2,3,4) # 2x2 matrixbroadcast(f, As...) -> Array (or f.(As...))Apply f element-wise over arrays, expanding singleton dimensions. The dot syntax f.(x, y) is the idiomatic form.
Parameters
| Name | Type | Description |
|---|---|---|
| f | Function | Function to apply element-wise. |
| As... | Any... | Inputs; arrays broadcast over singleton dims. |
Returns
Array of results; fusing broadcasts (@.) avoids temporaries.
Example
sin.([0, π/2, π]) # [0.0, 1.0, ~0]
[1,2,3] .+ [10,20,30] # [11,22,33]
[1,2,3] .+ 10 # scalar broadcast -> [11,12,13]
@. sin(x)^2 + cos(x)^2 # fused, single loopmapreduce(f, op, A; [init]) -> AnyApply f to each element of A, then reduce with op. Equivalent to op(f(A[1]), op(f(A[2]), ...)) but fused into one pass.
Parameters
| Name | Type | Description |
|---|---|---|
| f | Function | Map function applied per element. |
| op | Function | Associative binary operator for reduction. |
| A | Iterable | Input collection. |
| init | Any (kw) | Optional initial value for op. |
Returns
The reduced result. Type matches init / op's output.
Example
mapreduce(abs, +, [-1, 2, -3]) # 6 (sum of abs)
mapreduce(uppercase, *, "abc") # "ABC"
sum(x -> x^2, 1:10) # 385 (sum of squares)
mapreduce(sin, +, 1:1000; init=0.0)sort(A; [dims=1], [alg], [lt=isless], [by], [rev=false]) -> ArrayReturn a sorted copy of A (or sort along dims). Use sort! for in-place sorting of a Vector.
Parameters
| Name | Type | Description |
|---|---|---|
| A | Array | Input array. |
| dims | Int (kw) | Dimension to sort along (default 1). |
| alg | Algorithm (kw) | Sort algorithm: InsertionSort, QuickSort, MergeSort, RadixSort. |
| lt | Function (kw) | Custom less-than comparator. |
| by | Function (kw) | Transform applied before comparison (e.g. by=abs). |
| rev | Bool (kw) | Reverse the sort order. |
Returns
A new sorted array (or sorted view of the same data).
Example
sort([3, 1, 2]) # [1, 2, 3]
sort([3, -1, 2]; by=abs) # [-1, 2, 3]
sort([1,2,3]; rev=true) # [3, 2, 1]
sort!([3,1,2]) # in-place sort
sortperm([30, 10, 20]) # [2, 3, 1] (indices)