grammar
C = horzcat(A,B) C = horzcat(A1,A2... ,An)Copy the code
The input parameters
A – First input
Scalar multidimensional arrays table | | | | | vector matrix schedule the first input, designated as a scalar, vector, matrix, multidimensional arrays, tables or schedule.
B – Second input
Scalar multidimensional arrays table | | | | | vector matrix timetable The second input, designated as a scalar, vector, matrix, multidimensional arrays, tables, or schedule.
The elements of B are concatenated along the second dimension to the end of the first input. The size of the input parameter must be compatible. For example, if the first input is a 3×2 matrix, then B must have 3 rows.
All table inputs must have unique variable names. If there are row names, they must be exactly the same (except in order).
All schedule inputs must have the same row time, and each column must have a different name.
A1, A2,… ,An – Input list
Comma-separated list
Specified as a comma-separated list, the list elements are concatenated in the order in which they appear in the list.
The input must have compatible sizes. For example, if A1 is a column vector of length m, the remaining inputs must have m rows each in order to be horizontally connected.
All table inputs must have unique variable names. If there are row names, they must be exactly the same (except in order).
All schedule inputs must have the same row time, and each column must have a different name.
instructions
When A and B have compatible sizes (all dimensions except the second need to match in length), C = horzcat(A,B) series B horizontally to the end of A.
C = horzcat (A1, A2,… ,An) horizontal series A1, A2… And the An.
Horzcat is equivalent to horizontally concatenating an array with square brackets. For example, when A and B are compatible arrays, [A,B] or [A B] is equal to horzcat(A,B).
The instance
Two matrices
Create two matrices and concatenate them horizontally – first using square bracket notation and then using horzcat concatenation.
>> A=magic(5);
>> A(4:5,:)=[]
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
>> B=magic(3)*100
B =
800 100 600
300 500 700
400 900 200
>> C=[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
>> D = horzcat(A,B)
D =
17 24 1 8 15 800 100 600
23 5 7 14 16 300 500 700
4 6 13 20 22 400 900 200
>>
Copy the code
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=3×2 table
Age Gender
___ ______
Thomas 5 M
Gordon 6 M
Percy 5 M
Copy the code
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' {' is Percy' 'Gordon' 'Thomas'}) B = 3 x3 table Height Weight for 45 45...... the... is Percy {}' NY ' Gordon 41 32 {'CA'} Thomas 40 34 {'MA'}Copy the code
And then we’re going to horizontally connect A and B. The order of the rows in C matches the order in A.
> > C = horzcat (A, B), C = 3 x5 table Age Gender Height Weight for _____ ______ ______ ______ __________ the Thomas 5 M 40 34 {'MA'} Gordon 6 M 41 32 {'CA'} Percy 5 M 45 45 {'NY'}Copy the code
There are different types of dates
Concatenate the date character vector, string date, and date time into a date line. The result is a date-time row vector.
>> chardate = '2020-03-24';
>> strdate = "2020-04-19";
>> t = datetime('2020-05-10','InputFormat','yyyy-MM-dd');
>> C = horzcat(chardate,strdate,t)
C = 1x3 datetime
24-Mar-2020 19-Apr-2020 10-May-2020
Copy the code
String array
Concatenate three string arrays into an array.
>> A1 = ["str1"; "str2"];
>> A2 = ["str3"; "str4"];
>> A3 = ["str5"; "str6"];
>> C = horzcat(A1,A2,A3)
C = 2x3 string
"str1" "str3" "str5"
"str2" "str4" "str6"
Copy the code
A matrix in a cell array
Creates a cell array containing two matrices. To horizontally concatenate matrices in a cell array into a matrix.
>> M1 = [1 2; 3 4]; >> M2 = [5 6 7; 8 9 10]; >> A1 = {M1,M2}; >> C = horzcat(A1{:}) C = 2×5 1 2 6 7 3 4 8 9 10Copy the code
prompt
To construct text by horizontally concatenating strings, character vectors, or character vector cell arrays, use the strcat function.
To construct a delimited text from an array of character vector cells or strings, use the strjoin function.
Resource portal
- Pay attention to [be a gentle program ape] public account
- In [do a tender program ape] public account background reply [Python information] [2020 autumn recruit] can get the corresponding surprise oh!
“❤️ thank you.”
- Click “like” to support it, so that more people can see this content.
- Share your thoughts with me in the comments section, and record your thought process in the comments section.