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