HOWTO · MATLAB
How to Append Vector to 3D Matrix in MATLAB
This article will guide you through the ways to append a vector to a 3D matrix in MATLAB.
On this page
MATLAB stores a 3D array as rows, columns, and pages. To add a compatible page at the end, concatenate along dimension 3 with cat(3, A, page), or assign it with A(:,:,end+1) = page. The new page must have the same row and column dimensions as each existing page. A row vector is a complete page when each page has one row; a column vector is a complete page when each page has one column.
Understand 3D Matrix Pages and Size Compatibility
A 3D array has its row dimension first, column dimension second, and page dimension third. For example, an array with size 2-by-3-by-4 contains four pages, each with two rows and three columns. MATLAB displays those pages separately, and an element is addressed by row, column, and page indices such as A(1,2,3). To select a whole page, use A(:,:,k); for example, A(:,:,3) selects the third page.
Appending along the third dimension increases the page count while keeping the first two dimensions fixed. If A has size M-by-N-by-P, the page you add must be M-by-N, so the result has size M-by-N-by-(P+1). A vector can serve directly as that page only when its orientation and length match the page shape: a 1-by-N row vector fits a one-row page, while an M-by-1 column vector fits a one-column page. For a general M-by-N page, arrange the values into an M-by-N matrix first.
Append a Vector as a New Page with cat
Use cat(3, A, page) to join page to the end of A along the page dimension. The first argument selects the dimension; using cat(1, ...) adds rows, and cat(2, ...) adds columns. The remaining dimensions must match, which is why the vector below has two entries and the existing pages each have one row and two columns.
This Octave-compatible MATLAB example creates two pages, then adds the row vector as a third page:
A = reshape(1:4, 1, 2, 2);
page = [5 6];
A = cat(3, A, page);
disp(size(A));
disp(A(:,:,3));
1 2 3
5 6
The new array has three pages. Its third page contains the values from page in the same order. In MATLAB, cat() is useful when you want to create a new array value from the original array and the added page; assign the result back to A when you want to update that variable.
Append a Vector as a New Page with Indexing
Indexed assignment is an alternative to the cat method. Start with the original two-page A from the preceding example, before its cat line runs, and use A(:,:,end+1) = page; in place of that line. Do not run both append statements in sequence: the assignment extends the third dimension too, so running it after cat would add a fourth page. Here end refers to the last index along the third dimension, and end+1 selects the next page.
Use this form when updating A directly is clearest. The assigned page must match the existing row and column dimensions; arrange the vector first when its orientation or length does not fit the page shape.
Prepare a Vector That Matches the Page Size
If the vector is shorter than the page, decide what the remaining entries should represent before appending it. When zeros are meaningful, create a page with the required dimensions and place the vector values in the intended positions. For instance, for a 2-by-3 page whose first column contains a two-element column vector, a construction such as [values(:), zeros(2,2)] makes the other two columns zero. Then append that page with either method above.
Padding is a data choice, not a shape conversion that is always safe: the zeros become part of the stored page and may affect later calculations. If the missing values have another meaning, use the appropriate fill values or reshape/reorder the source data instead. Before concatenating or assigning, compare size(A,1) and size(A,2) with the first and second dimensions of the page. This makes a dimension mismatch easier to locate and keeps the intended row-column layout explicit.
Replace a Row, Column, or Page by Indexing
Replacing existing content is different from appending a page. For example, A(1,:,1) = replacementRow; replaces the first row of the first page, while A(:,:,1) = replacementPage; replaces an entire existing page. The replacement must have the same number of elements and compatible dimensions as the indexed region.
Use an existing index when the goal is to update data already in the array. Use end+1 in the third index when the goal is to add a page after the current last page. This distinction helps prevent an assignment from overwriting a page when you intended to preserve the existing data and append another one.