Main Content

vertcat

Concatenate arrays vertically

Description

example

C = vertcat(A,B)concatenatesBvertically to the end ofAwhenAandBhave compatible sizes (the lengths of the dimensions match except in the first dimension).

example

C = vertcat(A1,A2,…,An)concatenatesA1,A2, … ,Anvertically.

vertcatis equivalent to using square brackets for vertically concatenating arrays. For example,[A; B]is equal tovertcat(A,B)whenAandBare compatible arrays.

Examples

collapse all

创建two matrices and concatenate them vertically, first by using square bracket notation, and then by usingvertcat.

A = [1 2 3; 4 5 6]
A =2×31 2 3 4 5 6
B = [7 8 9]
B =1×37 8 9
C = [A; B]
C =3×31 2 3 4 5 6 7 8 9
D = vertcat(A,B)
D =3×31 2 3 4 5 6 7 8 9

创建a tableAwith three rows and five variables.

A = table([5;6;5],['M';'M';'M'],[45;41;40],[45;32;34],{'NY';'CA';'MA'},...'VariableNames',{'Age''Gender''Height''Weight''Birthplace'},...'RowNames',{'Thomas''Gordon''Percy'})
A=3×5 tableAge Gender Height Weight Birthplace ___ ______ ______ ______ __________ Thomas 5 M 45 45 {'NY'} Gordon 6 M 41 32 {'CA'} Percy 5 M 40 34 {'MA'}

创建a tableBwith the same variables asAexcept for order.

B = table(['F';'M';'F'],[6;6;5],{'AZ';'NH';'CO'},[31;42;33],[39;43;40],...'VariableNames',{'Gender''Age''Birthplace''Weight''Height'})
B=3×5 tableGender Age Birthplace Weight Height ______ ___ __________ ______ ______ F 6 {'AZ'} 31 39 M 6 {'NH'} 42 43 F 5 {'CO'} 33 40

Vertically concatenate tablesAandB. The variables ofCare in the same order as the variables ofAand default row names are used for the rows fromB.

C = vertcat(A,B)
C=6×5 tableAge Gender Height Weight Birthplace ___ ______ ______ ______ __________ Thomas 5 M 45 45 {'NY'} Gordon 6 M 41 32 {'CA'} Percy 5 M 40 34 {'MA'} Row4 6 F 39 31 {'AZ'} Row5 6 M 43 42 {'NH'} Row6 5 F 40 33 {'CO'}

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

chardate ='2016-03-24'; strdate ="2016-04-19"; t = datetime('2016-05-10','InputFormat','yyyy-MM-dd'); C = vertcat(chardate,strdate,t)
C =3x1 datetime24-Mar-2016 19-Apr-2016 10-May-2016

Concatenate three string arrays into a single array.

A1 = ["str1""str2"]; A2 = ["str3""str4"]; A3 = ["str5""str6"]; C = vertcat(A1,A2,A3)
C =3x2 string"str1" "str2" "str3" "str4" "str5" "str6"

创建a cell array containing two matrices. Vertically concatenate the matrices from the cell array into one matrix.

M1 = [1 2; 3 4]; M2 = [5 6; 7 8]; A1 = {M1,M2}; C = vertcat(A1{:})
C =4×21 2 3 4 5 6 7 8

Input Arguments

collapse all

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

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

  • The elements ofBare concatenated to the end of the first input along the first dimension. The sizes of the input arguments must be compatible. For example, if the first input is a matrix of size 3-by-2, thenBmust have 2 columns.

  • 你可以连接不同的有效组合t types. For more information, seeValid Combinations of Unlike Classes.

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

  • The inputs must have compatible sizes. For example, ifA1is a row vector of lengthm, then the remaining inputs must each havemcolumns to concatenate vertically.

  • 你可以连接不同的有效组合t types. For more information, seeValid Combinations of Unlike Classes.

Algorithms

When concatenating an empty array to a nonempty array,vertcatomits the empty array in the output. For example,vertcat([1; 2],[])returns the column vector[1; 2].

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

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a