This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Concatenate arrays horizontally


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


C = horzcat(A1,...,AN) horizontally concatenates arrays A1,...,AN. All arrays in the argument list must have the same number of rows.

  • If the inputs are multidimensional arrays, horzcat concatenates along the second dimension. The first and remaining dimensions must match.

  • If the inputs are tables, horzcat concatenates by matching row names when present, or by matching position for tables that do not have row names. All the table inputs must have unique variable names and the row names for all tables that have them must be identical, except for order.

    horzcat assigns values for the Description and UserData properties in C using the first nonempty value for the corresponding property in the tables A1,...,AN.

    If the inputs are timetables, the row times must be the same and all columns must have different names.

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


You can concatenate categorical arrays with cell arrays of character vectors. 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 character vectors.

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.

You can concatenate string arrays with numeric arrays, logical arrays, character vectors, or cell arrays. The non-string arrays are first converted to a string array, and the output is returned as a string array.

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


collapse all

Create a 3-by-5 matrix, A.

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

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22

Create a 3-by-3 matrix, B.

B = magic(3)*100
B =

   800   100   600
   300   500   700
   400   900   200

Horizontally concatenate A and B.

C = horzcat(A,B)
C =

    17    24     1     8    15   800   100   600
    23     5     7    14    16   300   500   700
     4     6    13    20    22   400   900   200

Create a table, A, with three rows and two variables.

A = table([5;6;5],['M';'M';'M'],...
    'VariableNames',{'Age' 'Gender'},...
    'RowNames',{'Thomas' 'Gordon' 'Percy'})
A = 

              Age    Gender
              ___    ______

    Thomas    5      M     
    Gordon    6      M     
    Percy     5      M     

Create a table, B, with three rows and three variables.

B = table([45;41;40],[45;32;34],{'NY';'CA';'MA'},...
    'VariableNames',{'Height' 'Weight' 'Birthplace'},...
    'RowNames',{'Percy' 'Gordon' 'Thomas'})
B = 

              Height    Weight    Birthplace
              ______    ______    __________

    Percy     45        45        'NY'      
    Gordon    41        32        'CA'      
    Thomas    40        34        'MA'      

Horizontally concatenate A and B.

C = horzcat(A,B)
C = 

              Age    Gender    Height    Weight    Birthplace
              ___    ______    ______    ______    __________

    Thomas    5      M         40        34        'MA'      
    Gordon    6      M         41        32        'CA'      
    Percy     5      M         45        45        'NY'      

The order of rows in C matches the order in A.

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Introduced before R2006a

Was this topic helpful?