cat
stacks vectors matrices or arrays into an hypermatrix or hyperarray
Syntax
y = cat(dim, A1, A2, ...,An)
Arguments
- dim
integer>0: index of the dimension along which input arrays must be stacked.
- A1, A2,..An
scalars, vectors, matrices or hypermatrices of compatible data types, or cells arrays.
A1,A2,...,Anmust be of the same sizes, except in the direction #dimof stacking :size(A1,i)=size(A2,i)=...=size(An,i)for alli <> dim.- y
a vector, matrix, hypermatrix, or ND-array of cells or of structures. For all
i <> dim,size(y,i)==size(A1,i), andsize(y,dim) == sum(size(Ai, dim))).
Description
y=cat(dim,A1,A2,...,An) : y is the result of the
concatenation of the input arguments A1,A2,...,An, along the
dimension #dim.
cat(1, A1, A2, A3) is equivalent to [A1 ; A2 ; A3].
cat(2, A1, A2, A3) is equivalent to [A1 A2 A3].
Brackets are faster and must be preferred in both cases.
When input arrays have not all the same data type, automatical conversion rules similar
to the [,] and [;] ones are applied, as described
in the [brackets] page.
cat() is useful mainly for dim > 2, to build an
hypermatrix or hypercell.
Examples
Example #1: Building a vector along dim #3
cat(3, 4, -1, 3)
--> cat(3, 4, -1, 3) ans = (:,:,1) 4. (:,:,2) -1. (:,:,3) 3.
Example #2: Building a matrix of text of size(1,3,2):
cat(3, ["a" "aa" "aaa"], ["b" "bb" "bbb"])
--> cat(3, ["a" "aa" "aaa"], ["b" "bb" "bbb"]) ans = (:,:,1) !a aa aaa ! (:,:,2) !b bb bbb !
Example #3: Stacking separate R G B layers of a mini RGB image:
R = uint8(grand(2,4,"uin",0,255)) G = uint8(grand(2,4,"uin",0,255)) B = uint8(grand(2,4,"uin",0,255)) cat(3, R, G, B)
--> R = uint8(grand(2,4,"uin",0,255)) R = 142 8 11 234 191 249 252 51 --> G = uint8(grand(2,4,"uin",0,255)) G = 255 246 104 89 152 71 112 17 --> B = uint8(grand(2,4,"uin",0,255)) B = 170 182 39 197 115 108 16 51 --> cat(3, R, G, B) ans = (:,:,1) 142 8 11 234 191 249 252 51 (:,:,2) 255 246 104 89 152 71 112 17 (:,:,3) 170 182 39 197 115 108 16 51
Example #4: Building an hypercell:
--> A1 = {%T "abc" ; (1-%z)^2, %pi}
A1 =
[1x1 boolean ] [1x1 string ]
[1x1 polynomial] [1x1 constant]
--> A2 = {%s^2, gda(); %F, list(-5, "hello")}
A2 =
[1x1 polynomial] [1x1 handle]
[1x1 boolean ] [ list ]
--> cat(3, A1, A2)
ans =
(:,:,1)
[1x1 boolean ] [1x1 string ]
[1x1 polynomial] [1x1 constant]
(:,:,2)
[1x1 polynomial] [1x1 handle]
[1x1 boolean ] [ list ]
See also
- brackets [..] — Concatenation. Recipients of an assignment. Results of a function
- lstcat — list concatenation
- permute — permutes the dimensions of an array
- matrix — reshapes an array with the same number and order of components
History
| Version | Description |
| 2023.1 | Arrays of compatible types accepted, according to [,] and [;] transtyping rules. |
| Report an issue | ||
| << Shape tests | Matrix - shaping | circshift >> |