How to concatenate matrices horizontally?

前端 未结 2 544
孤街浪徒
孤街浪徒 2021-01-26 02:36

I have two 3-by-3 matrices:

A= [ 1 2 3 
     1 1 1
     0 1 1] 

B= [ 1 2 1 
     1 1 1
     2 2 2]

How do I concatenate the

相关标签:
2条回答
  • 2021-01-26 02:54

    There are a few possibilities here. The easiest, and most common one:

    concat = [A, B]
    

    The following is considered more robust by some, (because one might, on accident, do concat = [A; B], which would concatenate them vertically):

    concat = horzcat(A, B)
    
    0 讨论(0)
  • 2021-01-26 03:13

    Simply do:

    concat = [A B]; 
    

    This will make a new matrix that pieces A and B together horizontally (i.e. concatenates).

    Another possibility is to use cat where you specify the second dimension (column-wise) to concatenate the two matrices together.

    concat = cat(2, A, B);
    

    Alternatively you can use horzcat as alluded by a few people here. This essentially is syntactic sugar for cat in the second dimension.

    concat = horzcat(A, B);
    
    0 讨论(0)
提交回复
热议问题