Main Content

horzcat

Concatenate arrays horizontally

Description

example

C = horzcat(A,B) concatenates B horizontally to the end of A when A and B have compatible sizes (the lengths of the dimensions match except in the second dimension).

example

C = horzcat(A1,A2,…,An) concatenates A1, A2, … , An horizontally.

horzcat is equivalent to using square brackets to horizontally concatenate or append arrays. For example, [A,B] and [A B] is the same as horzcat(A,B) when A and B are compatible arrays.

Examples

collapse all

Concatenate two matrices horizontally.

Create two matrices, and horizontally append the second matrix to the first by using square bracket notation.

A = [1 2; 3 4]
A = 2×2

     1     2
     3     4

B = [4 5 6; 7 8 9]
B = 2×3

     4     5     6
     7     8     9

C = [A,B]
C = 2×5

     1     2     4     5     6
     3     4     7     8     9

Now, horizontally append the second matrix to the first by using horzcat.

D = horzcat(A,B)
D = 2×5

     1     2     4     5     6
     3     4     7     8     9

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=3×2 table
              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=3×3 table
              Height    Weight    Birthplace
              ______    ______    __________

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

Horizontally concatenate A and B. The order of rows in C matches the order in A.

C = horzcat(A,B)
C=3×5 table
              Age    Gender    Height    Weight    Birthplace
              ___    ______    ______    ______    __________

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

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

chardate = '2016-03-24';
strdate = "2016-04-19";
t = datetime('2016-05-10','InputFormat','yyyy-MM-dd');
C = horzcat(chardate,strdate,t)
C = 1x3 datetime
   24-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 = horzcat(A1,A2,A3)
C = 2x3 string
    "str1"    "str3"    "str5"
    "str2"    "str4"    "str6"

Create a cell array containing two matrices. Horizontally concatenate the matrices from the cell array into one matrix.

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

     1     2     5     6     7
     3     4     8     9    10

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 of B are concatenated to the end of the first input along the second 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 3 rows.

  • All table inputs must have unique variable names. When present, row names must be identical, except for order.

  • 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.

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, if A1 is a column vector of length m, then the remaining inputs must each have m rows to concatenate horizontally.

  • All table inputs must have unique variable names. When present, row names must be identical, except for order.

  • 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

For table inputs, horzcat concatenates by matching row names when present, or by matching table positions. horzcat assigns values for the Description and UserData properties of the output using the first nonempty values of the corresponding properties of the input.

When concatenating an empty array to a nonempty array, horzcat omits the empty array in the output. For example, horzcat([1 2],[]) returns the row vector [1 2].

If all input arguments are empty and have compatible sizes, then horzcat returns an empty array whose size is equal to the output size as when the inputs are nonempty. For example, horzcat(zeros(0,1),zeros(0,2)) returns a 0-by-3 empty array. If the input sizes are not compatible, then horzcat returns 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