horzcat - Concatenate arrays horizontally - MATLAB (original) (raw)

Concatenate arrays horizontally

Syntax

Description

C = horzcat([A](#mw%5Fb43a91e6-10ed-4d61-b3bc-e49e62ca5a2f),[B](#mw%5F76332210-81a3-4a0c-b91b-4f64966ebfd3)) concatenates B horizontally to the end of A whenA and B have compatible sizes (the lengths of the dimensions match except in the second dimension).

example

C = horzcat([A1,A2,…,An](#mw%5Fec54f37f-3a18-45ad-9bf0-c855d3c878cc)) concatenatesA1, 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 andB are compatible arrays.

example

Examples

collapse all

Concatenate two matrices horizontally.

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

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 = 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=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 = 1×3 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 = 2×3 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.

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

Tips

Algorithms

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, thenhorzcat 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, thenhorzcat returns a 0-by-0 empty array.

Extended Capabilities

expand all

Thehorzcat function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

The horzcat function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a