how to append a matrix in matlab

To append matrices in Matlab, you can use concatenation. There are two types of concatenation: horizontal concatenation and vertical concatenation.

Horizontal concatenation is used to append matrices horizontally, side by side. This can be done using the square bracket notation:

main.m
A = [1 2; 3 4]; %matrix A
B = [5 6; 7 8]; %matrix B
C = [A B]; %Horizontally concatenate A and B
97 chars
4 lines

The resulting matrix C will be a 2x4 matrix.

Vertical concatenation is used to append matrices vertically, one on top of the other. This can also be done using the square bracket notation:

main.m
D = [A; B]; %Vertically concatenate A and B
44 chars
2 lines

The resulting matrix D will be a 4x2 matrix.

In both cases, it's important to make sure the matrices have the same dimensions along the dimension you wish to concatenate.

gistlibby LogSnag