How to Append Vector to 3D Matrix in MATLAB

Ammar Ali Feb 12, 2024
  1. 3D Matrix in MATLAB
  2. Append a Vector to a 3D Matrix in MATLAB Using Concatenation
  3. Append a Vector to a 3D Matrix in MATLAB Using Indexing
  4. Conclusion
How to Append Vector to 3D Matrix in MATLAB

MATLAB is a powerful numerical computing environment that is widely used in various scientific and engineering applications.

When working with multidimensional arrays in MATLAB, it is common to encounter scenarios where you need to append a vector to a 3D matrix. This process can be essential for expanding your data or incorporating new information into an existing dataset.

In this article, we will guide you through the ways to append a vector to a 3D matrix in MATLAB.

3D Matrix in MATLAB

A 3D matrix or array is different from a 2D matrix or array. In a 2D matrix, we encounter two dimensions – rows and columns.

The first dimension signifies the row, while the second denotes the column. However, when delving into the realm of 3D matrices, an additional dimension emerges, introducing a new layer of complexity.

The first two dimensions remain analogous to those of a 2D matrix, while the third dimension is referred to as pages or sheets.

3d matrix

In a 2D matrix, elements are accessed using row and column indices. Conversely, a 3D matrix requires the additional specification of a page number.

For example, the first element in a 3D matrix is located at (1,1,1). This distinction is crucial when working with data spread across multiple dimensions.

Append a Vector to a 3D Matrix in MATLAB

Appending a vector to the end of a 3D matrix involves considering the size of the vector in relation to the third dimension or pages. The vector’s length should match the size of each page in the 3D matrix.

This ensures seamless integration without compromising the integrity of the data structure.

Let’s create a basic 3D matrix in MATLAB to illustrate these concepts:

clc
clear

% Creating a 3D matrix with 1 row, 2 columns, and 2 pages
MyMatrix = ones(1, 2, 2)

The resulting output showcases a 3D matrix comprising two pages, each containing a 2D matrix with 1 row and 2 columns. This exemplifies how MATLAB organizes and displays 3D arrays relative to their third dimension.

MyMatrix(:,:,1) =

     1     1


MyMatrix(:,:,2) =

     1     1

Append a Vector to a 3D Matrix in MATLAB Using Concatenation

The cat() function in MATLAB is a versatile tool for concatenating arrays along specified dimensions. When it comes to appending a vector to a 3D matrix, the syntax for the cat() function involves three main parameters:

C = cat(dim, A, B);

Where:

  • dim: The dimension along which concatenation occurs.
  • A: The original 3D matrix.
  • B: The vector or matrix to be appended.

In our context, we aim to append a vector to the 3D matrix, so dim would be the third dimension. The cat() function essentially creates a new page in the 3D matrix to accommodate the appended vector.

Let’s consider a scenario where we have a 3D matrix (originalMatrix) and want to append a vector (newVector) to it. In our example, to ensure successful concatenation, we reshape the vector using the repmat() function, adjusting its dimensions to match the third dimension of the matrix.

newVectorReshaped = repmat(newVector, 1, size(originalMatrix, 2), size(originalMatrix, 3));

Finally, we use cat() to append the reshaped vector along the third dimension:

newMatrix = cat(1, originalMatrix, newVectorReshaped);

Complete Code Example:

clc
clear

originalMatrix = randi([1, 10], [3, 4, 2]);
newVector = [11; 12; 13];

newVectorReshaped = repmat(newVector, 1, size(originalMatrix, 2), size(originalMatrix, 3));

newMatrix = cat(1, originalMatrix, newVectorReshaped);

disp('Original 3D Matrix:');
disp(originalMatrix);

disp('Modified 3D Matrix after Appending Vector:');
disp(newMatrix);

In the provided code, we start by creating a random 3D matrix (originalMatrix) using the randi() function. This function generates random integer values within the range of 1 to 10 and constructs a 3D matrix with dimensions 3x4x2. This means the matrix has three rows, four columns, and two pages.

Next, we define a vector (newVector) that we intend to append to the matrix. The goal is to append this vector to the originalMatrix.

To achieve this, a reshaped version of newVector is created using the repmat function. This function replicates the newVector along the first dimension (rows) to match the number of rows in the originalMatrix.

The size of replication along the other dimensions is determined by the size of the originalMatrix. In this case, we replicate it across the second dimension (columns) and the third dimension (pages).

The resulting reshaped vector, named newVectorReshaped, is then concatenated to the originalMatrix along the first dimension using the cat function. The first argument to cat is 1, signifying concatenation along the first dimension (rows).

This operation effectively appends the reshaped vector as additional rows to the original 3D matrix, creating a new matrix named newMatrix.

Finally, the code displays the original 3D matrix and the modified 3D matrix after appending the vector using the disp function.

Code Output:

Original 3D Matrix:

(:,:,1) =

     7     3     7     7
     5     8     2     8
     6     2     4     1


(:,:,2) =

    10     5     6     8
     8     5     6     7
     5     4     9     4

Modified 3D Matrix after Appending Vector:

(:,:,1) =

     7     3     7     7
     5     8     2     8
     6     2     4     1
    11    11    11    11
    12    12    12    12
    13    13    13    13


(:,:,2) =

    10     5     6     8
     8     5     6     7
     5     4     9     4
    11    11    11    11
    12    12    12    12
    13    13    13    13

Adjustments made to the dimensions ensure seamless concatenation, maintaining the integrity of the 3D matrix structure.

Append a Vector to a 3D Matrix in MATLAB Using Indexing

In addition to using the concatenation function cat() to append a vector to a 3D matrix in MATLAB, another powerful technique involves utilizing indexing. This method allows for more direct control over the placement of the vector within the matrix.

MATLAB’s indexing capabilities provide a flexible and intuitive way to access and modify elements within arrays. To append a vector to a 3D matrix using indexing, we need to specify the position where the vector should be inserted.

In our case, we aim to extend the matrix along its third dimension. The syntax for appending a vector along the third dimension is as follows:

originalMatrix(:, :, end+1) = newVectorReshaped;

Where:

  • originalMatrix: The original 3D matrix.
  • newVectorReshaped: The vector to be appended after ensuring its dimensions match the target matrix.

The use of end+1 in indexing refers to the last element along the specified dimension, effectively adding a new element.

Complete Code Example:

clc
clear

originalMatrix = randi([1, 10], [3, 4, 2]);
disp('Original 3D Matrix:');
disp(originalMatrix);

newVector = [11; 12; 13];

newVectorReshaped = repmat(newVector, 1, size(originalMatrix, 2), 1);

originalMatrix(:, :, end+1) = newVectorReshaped;

disp('3D Matrix After Appending Vector:');
disp(originalMatrix);

In this example, we start by creating a random 3D matrix (originalMatrix) using the randi() function, displaying it to provide a reference.

Next, a vector (newVector) is defined with values [11; 12; 13]. This vector is the one we intend to append to the original 3D matrix.

In order to ensure compatibility for indexing, the vector is reshaped using repmat() to match the dimensions of the third dimension of the matrix. This ensures that the vector has the correct size for indexing. The reshaped vector is stored in newVectorReshaped.

The vector is then appended to the 3D matrix using indexing. The end+1 notation refers to the last page along the third dimension, effectively adding a new page to the matrix and filling it with the values from newVectorReshaped.

Finally, we use disp() to showcase the modified 3D matrix in the command window.

Code Output:

Original 3D Matrix:

(:,:,1) =

     5     4     9     7
     7     7     9     6
     8     5     3     6


(:,:,2) =

     9     2     5     7
     3    10     7     6
     4     7     6     8

3D Matrix After Appending Vector:

(:,:,1) =

     5     4     9     7
     7     7     9     6
     8     5     3     6


(:,:,2) =

     9     2     5     7
     3    10     7     6
     4     7     6     8


(:,:,3) =

    11    11    11    11
    12    12    12    12
    13    13    13    13

The output displays both the original 3D matrix and the modified matrix after appending the vector. The indexing approach directly modifies the matrix, providing an efficient way to extend the 3D structure without using concatenation functions.

Conclusion

In conclusion, we have explored two distinct methods for appending a vector to a 3D matrix in MATLAB: concatenation and indexing. Both approaches offer effective solutions depending on the specific requirements and coding preferences.

The concatenation method demonstrated through the use of the cat() function allows for a straightforward and concise way to extend a 3D matrix along a specified dimension. This method is particularly useful when the dimensions of the vector and the target matrix align seamlessly.

On the other hand, the indexing method provides a direct and intuitive way to append a vector along a specific dimension of a 3D matrix. By utilizing the end+1 notation, indexing allows for the addition of a new element along the targeted dimension, resulting in a modified matrix.

Choosing between these methods depends on the nature of the task at hand. Concatenation might be preferable when dealing with matrices of consistent dimensions, while indexing offers a more direct and explicit control over the manipulation of matrix elements.

Regardless of the chosen method, both approaches provide versatile tools for extending and modifying 3D matrices in MATLAB. Understanding these techniques equips users with the flexibility to adapt their code to diverse scenarios, ensuring efficient and effective matrix manipulations in MATLAB.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Matrix