cat
cat(dims, A...)
Concatenate the input arrays along the specified dimensions in the iterable dims
. For dimensions not in dims
, all input arrays should have the same size, which will also be the size of the output array along that dimension. For dimensions in dims
, the size of the output array is the sum of the sizes of the input arrays along that dimension. If dims
is a single number, the different arrays are tightly stacked along that dimension. If dims
is an iterable containing several dimensions, this allows to construct block diagonal matrices and their higher-dimensional analogues by simultaneously increasing several dimensions for every new input array and putting zero blocks elsewhere. For example, cat([1,2], matrices...)
builds a block diagonal matrix, i.e. a block matrix with matrices[1]
, matrices[2]
, ... as diagonal blocks and matching zero blocks away from the diagonal.
Examples
julia> foo = Array(Int8,2,2)
2x2 Array{Int8,2}:
2 0
0 0
julia> bar = Array(Int8,2,2)
2x2 Array{Int8,2}:
-93 -111
62 5
julia> cat(1,foo,bar)
4x2 Array{Int8,2}:
2 0
0 0
-93 -111
62 5
julia> cat(2,foo,bar)
2x4 Array{Int8,2}:
2 0 -93 -111
0 0 62 5
julia> A = [1 2 3; 4 5 6];
julia> B = [7 8 9; 10 11 12];
julia> cat(1, A, B)
4×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
10 11 12
julia> cat(2, A, B)
2×6 Array{Int64,2}:
1 2 3 7 8 9
4 5 6 10 11 12
julia> C = [13 14; 15 16];
julia> cat([1, 2], A, B, C)
6×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 0
15 16 0
The cat
function in Julia is used to concatenate or stack arrays along specified dimensions. Here are some examples of how to use it:
-
Concatenate arrays vertically (along dimension 1):
julia> A = [1 2 3; 4 5 6]; julia> B = [7 8 9; 10 11 12]; julia> cat(1, A, B) 4×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9 10 11 12
This example concatenates arrays
A
andB
vertically, resulting in a 4x3 array. -
Concatenate arrays horizontally (along dimension 2):
julia> A = [1 2 3; 4 5 6]; julia> B = [7 8 9; 10 11 12]; julia> cat(2, A, B) 2×6 Array{Int64,2}: 1 2 3 7 8 9 4 5 6 10 11 12
This example concatenates arrays
A
andB
horizontally, resulting in a 2x6 array. - Construct block diagonal matrices:
julia> A = [1 2 3; 4 5 6]; julia> B = [7 8 9; 10 11 12]; julia> C = [13 14; 15 16]; julia> cat([1, 2], A, B, C) 6×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 15 16 0
This example constructs a block diagonal matrix by concatenating arrays
A
,B
, andC
along dimensions 1 and 2.
Note: Ensure that the arrays being concatenated have compatible dimensions along the specified dimensions.
See Also
Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.