vertcat

Concatenate arrays vertically

Syntax

C = vertcat(A1,...,AN)

Description

C = vertcat(A1,...,AN) vertically concatenates arrays A1,...,AN. All arrays in the argument list must have the same number of columns.

  • If the inputs are multidimensional arrays, vertcat concatenates N-dimensional arrays along the first dimension. The remaining dimensions must match.

  • If the inputs are tables, vertcat concatenates by matching variable names. Variable names for all tables must be identical, except for order. Row names, when present, must be unique across tables.

    vertcat fills in default row names when some of the inputs have names and some do not. vertcat assigns values for each table property (except for RowNames) using the first nonempty value for the corresponding property in the tables A1,...,AN.

vertcat also concatenates character strings. Each string being concatenated must have the same number of characters.

MATLAB® calls C = vertcat(A1, A2, ...) for the syntax C = [A1; A2; ...] when any of the inputs are an object.

Tips

You can concatenate categorical arrays with cell arrays of strings. For more information, see Combine Categorical Arrays.

If all the input arrays are ordinal categorical arrays, they must have the same sets of categories including their order. For more information, see Ordinal Categorical Arrays.

You can concatenate datetime arrays with cell arrays of strings.

You can concatenate duration arrays and calendar duration arrays. The result is a calendar duration array.

You can concatenate duration or calendar duration arrays with numeric arrays. Prior to concatenation, MATLAB converts the numeric array to an array of equivalent days using the days function.

For information on combining unlike integer types, integers with nonintegers, cell arrays with non-cell arrays, or empty matrices with other elements, see Valid Combinations of Unlike Classes.

Examples

expand all

Vertically Concatenate Two Matrices

Create a 5-by-3 matrix, A.

A = magic(5);
A(:, 4:5) = []
A =

    17    24     1
    23     5     7
     4     6    13
    10    12    19
    11    18    25

Create a 3-by-3 matrix, B.

B = magic(3)*100
B =

   800   100   600
   300   500   700
   400   900   200

Vertically concatenate A and B.

C = vertcat(A,B)
C =

    17    24     1
    23     5     7
     4     6    13
    10    12    19
    11    18    25
   800   100   600
   300   500   700
   400   900   200

Vertically Concatenate Two Tables

Create a table, A, with 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 = 

              Age    Gender    Height    Weight    Birthplace
              ---    ------    ------    ------    ----------
    Thomas    5      M         45        45        'NY'      
    Gordon    6      M         41        32        'CA'      
    Percy     5      M         40        34        'MA'      

Create a table, B, with the same variables as A except 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 = 

    Gender    Age    Birthplace    Weight    Height
    ------    ---    ----------    ------    ------
    F         6      'AZ'          31        39    
    M         6      'NH'          42        43    
    F         5      'CO'          33        40    

Vertically concatenate tables A and B.

C = vertcat(A,B)
C = 

              Age    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'      

The variables of C are in the same order as the variables of A and default row names are used for the rows from B.

See Also

|

Was this topic helpful?