cat - Concatenate arrays - MATLAB (original) (raw)

Syntax

Description

C = cat([dim](#d126e176542),[A](#mw%5F8e40eb41-9d14-41da-9357-a1cbac7b4211),[B](#mw%5Fcd6decd9-9f5d-491f-b579-a64d3bfef5e7)) concatenates B to the end of A along dimensiondim when A and B have compatible sizes (the lengths of the dimensions match except for the operating dimensiondim).

example

C = cat([dim](#d126e176542),[A1,A2,…,An](#mw%5F1751ec86-3e52-4609-8282-397a6ac6bb25)) concatenates A1, A2, … , An along dimension dim.

You can use the square bracket operator [] to concatenate or append arrays. For example, [A,B] and [A B] concatenates arrays A and B horizontally, and [A; B] concatenates them vertically.

example

Examples

collapse all

Two Matrices

Concatenate two matrices vertically, then horizontally.

Create two matrices, and vertically append the second matrix to the first.

A = 3×3

 1     1     1
 1     1     1
 1     1     1

B = 3×3

 0     0     0
 0     0     0
 0     0     0

C1 = 6×3

 1     1     1
 1     1     1
 1     1     1
 0     0     0
 0     0     0
 0     0     0

Now, horizontally append the second matrix to the first.

C2 = 3×6

 1     1     1     0     0     0
 1     1     1     0     0     0
 1     1     1     0     0     0

3-D Arrays

Create two 3-D arrays and concatenate them along the third dimension. The lengths of the first and second dimensions in the resulting array match the corresponding lengths in the input arrays, while the third dimension expands.

A = rand(2,3,4); B = rand(2,3,5); C = cat(3,A,B); szC = size(C)

Expand Tables

Create a table and add a row using a cell array.

LastName = {'Sanchez';'Johnson';'Li';'Diaz'}; Age = [38;43;38;40]; T1 = table(LastName,Age)

T1=4×2 table LastName Age ___________ ___

{'Sanchez'}    38 
{'Johnson'}    43 
{'Li'     }    38 
{'Diaz'   }    40 

Trow = {'Brown',49}; T2 = cat(1,T1,Trow)

T2=5×2 table LastName Age ___________ ___

{'Sanchez'}    38 
{'Johnson'}    43 
{'Li'     }    38 
{'Diaz'   }    40 
{'Brown'  }    49 

Dates with Different Types

Concatenate a date character vector, a string date, and a datetime into a single column of dates. The result is a datetime vector.

chardate = '2016-03-24'; strdate = "2016-04-19"; t = datetime('2016-05-10','InputFormat','yyyy-MM-dd'); C = cat(1,chardate,strdate,t)

C = 3x1 datetime 24-Mar-2016 19-Apr-2016 10-May-2016

Matrices in a Cell Array

Create a cell array containing two matrices, and concatenate the matrices both vertically and horizontally.

M1 = [1 2; 3 4]; M2 = [5 6; 7 8]; A1 = {M1,M2}; Cvert = cat(1,A1{:})

Cvert = 4×2

 1     2
 3     4
 5     6
 7     8

Chorz = 2×4

 1     2     5     6
 3     4     7     8

Input Arguments

collapse all

dim — Dimension to operate along

positive integer scalar

Dimension to operate along, specified as a positive integer scalar. For example, ifA and B are both 2-by-2 matrices, thencat(1,A,B) concatenates vertically creating a 4-by-2 matrix.cat(2,A,B) concatenates horizontally creating a 2-by-4 matrix.

dim must be either 1 or 2 for table or timetable input.

A — First input

scalar | vector | matrix | multidimensional array | table | timetable

First input, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

B — Second input

scalar | vector | matrix | multidimensional array | table | timetable

Second input, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

A1,A2,…,An — List of inputs

comma-separated list

List of inputs, specified as a comma-separated list of arrays to concatenate in the order they are specified.

Tips

Algorithms

When concatenating an empty array to a nonempty array, cat omits the empty array in the output. For example, cat(2,[1 2],[]) returns the row vector [1 2].

If all input arguments are empty and have compatible sizes, then cat returns an empty array whose size is equal to the output size as when the inputs are nonempty. For example, cat(2,zeros(0,1),zeros(0,2)) returns a 0-by-3 empty array.

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

Thecat function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The cat function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a