Documentation Center

  • Trial Software
  • Product Updates

horzcat

Concatenate arrays horizontally

Syntax

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

Description

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.

MATLAB® calls C = horzcat(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.

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

Horizontally Concatenate Two Matrices

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

Horizontally Concatenate Two Tables

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.

See Also

| | | |

Was this topic helpful?