Matlab series matrix function [horzcat] detailed analysis

grammar

C = horzcat(A,B)
C = horzcat(A1,A2,…,An)

Input parameters

A-first input

Scalar | Vector | Matrix | Multidimensional Array | Table | Timetable The
first input, specified as a scalar, vector, matrix, multidimensional array, table or timetable.

B-second input

Scalar | Vector | Matrix | Multidimensional Array | Table | Timetable The
second input, specified as a scalar, vector, matrix, multidimensional array, table or timetable.

The elements of B are concatenated along the second dimension to the end of the first input. The size of the input parameters must be compatible. For example, if the first input is a matrix of size 3×2, then B must have 3 rows.

All table entries must have unique variable names. If there are row names, they must be exactly the same (except for the order).

All timetable entries must have the same row time, and each column must have a different name.

A1,A2,…,An-input list

Comma separated list

The input list is specified as a comma-separated list, and the list elements will be concatenated according to their order in the list.

The input must have a compatible size. For example, if A1 is a column vector of length m, the remaining inputs must each have m rows to be horizontally concatenated.

All table entries must have unique variable names. If there are row names, they must be exactly the same (except for the order).

All timetable entries must have the same row time, and each column must have a different name.

Description

When A and B have compatible sizes (except for the second dimension, the lengths of other dimensions need to match), C = horzcat(A,B)connect B horizontally to the end of A.

C = horzcat(A1,A2,…,An) A1, A2,..., An are connected in series horizontally.

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

Instance

Two matrices

Create two matrices and concatenate them horizontally-first concatenate them using square bracket notation, then concatenate them using horzcat.

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

>> 

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   

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'}  

Then connect A and B horizontally. The order of the rows in C matches the order of 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'}  

Have different types of dates

Concatenate date character vector, string date, and date time into a date row. The result is a datetime 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

String array

Concatenate three string arrays into one array.

>> A1 = ["str1"; "str2"];
>> A2 = ["str3"; "str4"];
>> A3 = ["str5"; "str6"];
>> C = horzcat(A1,A2,A3)

C = 2x3 string
    "str1"    "str3"    "str5"
    "str2"    "str4"    "str6"

Matrix in cell array

Create a cell array containing two matrices. Concatenate the matrices in the cell array horizontally 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     5     6     7
     3     4     8     9    10

prompt

Through to construct a series of text strings, character or character vector Vector cellular array level , using the strcatfunction.

To the character array or vector of cellular text string array configuration section delimited , use strjoinfunction.

Resource portal

  1. attention【Be a tender programmer】No public
  2. in【Be a tender programmer】Public account backstage reply 【python information】【2020 Autumn Recruitment】You can get the corresponding surprise!

「❤️ Thank you everyone」

  1. Like to support it, so that more people can see this content (If you don’t like it, it’s all hooligans-_-)
  2. Welcome to share your thoughts with me in the message area, and you are also welcome to record your thought process in the message area.

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/108972189