Concatenate arrays - MATLAB cat (2024)

Concatenate arrays

collapse all in page

Syntax

C = cat(dim,A,B)

C = cat(dim,A1,A2,…,An)

Description

example

C = cat(dim,A,B) concatenates B to the end of A along dimension dim when A and B have compatible sizes (the lengths of the dimensions match except for the operating dimension dim).

example

C = cat(dim,A1,A2,…,An) 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.

Examples

collapse all

Two Matrices

Open Live Script

Concatenate two matrices vertically, then horizontally.

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

A = ones(3)
A = 3×3 1 1 1 1 1 1 1 1 1
B = zeros(3)
B = 3×3 0 0 0 0 0 0 0 0 0
C1 = cat(1,A,B)
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

Open Live Script

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)
szC = 1×3 2 3 9

Expand Tables

Open Live Script

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

Open Live Script

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

Open Live Script

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 = cat(2,A1{:})
Chorz = 2×4 1 2 5 6 3 4 7 8

Input Arguments

collapse all

dimDimension to operate along
positive integer scalar

Dimension to operate along, specified as a positive integer scalar. For example, if A and B are both 2-by-2 matrices, then cat(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.

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

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

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

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

  • The elements of B are concatenated to the end of the first input along the operating dimension. The sizes of the input arguments must be compatible. For example, if the first input is a matrix of size 3-by-2, then B must have 2 columns to concatenate vertically, and 3 rows to concatenate horizontally.

  • When concatenating horizontally, all table inputs must have unique variable names. When present, row names must be identical, except for order. Similarly, all timetable inputs must have the same row times and all columns must have different names.

  • You can concatenate valid combinations of different types. For more information, see Valid Combinations of Unlike Classes.

A1,A2,…,AnList of inputs
comma-separated list

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

  • The inputs must have compatible sizes. For example, if A1 is a row vector of length m, then the remaining inputs must each have m columns to concatenate vertically.

  • When concatenating horizontally, all table inputs must have unique variable names. When present, row names must be identical, except for order. Similarly, all timetable inputs must have the same row times and all columns must have different names.

  • You can concatenate valid combinations of different types. For more information, see Valid Combinations of Unlike Classes.

Tips

  • To construct text by horizontally concatenating strings, character vectors, or cell arrays of character vectors, use the strcat function.

  • To construct a single piece of delimited text from a cell array of character vectors or a string array, use the strjoin function.

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

The cat function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

See Also

vertcat | horzcat | strcat | strjoin

Topics

  • Creating, Concatenating, and Expanding Matrices
  • Valid Combinations of Unlike Classes
  • Combine Categorical Arrays
  • Concatenating Objects of Different Classes
  • Concatenation Methods

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Concatenate arrays - MATLAB cat (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Concatenate arrays - MATLAB cat (2024)
Top Articles
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6044

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.