concat two matrices in matlab

In order to concatenate two matrices "A" and "B" in MATLAB, we can use the 'vertcat' and 'horzcat' functions.

Here is an example of how to concatenate two matrices horizontally:

main.m
A = [1 2 3; 4 5 6; 7 8 9];
B = [11 12 13; 14 15 16; 17 18 19];
C = horzcat(A,B);
81 chars
4 lines

And here is an example of how to concatenate two matrices vertically:

main.m
A = [1 2 3; 4 5 6; 7 8 9];
B = [11 12 13; 14 15 16; 17 18 19];
C = vertcat(A,B);
81 chars
4 lines

Note that in both cases, the matrices must have the same number of rows or columns (depending on whether you are performing a horizontal or vertical concatenation).

related categories

gistlibby LogSnag