How to Add a Matrix to a Matrix in MATLAB
Operations on multidimensional data such as matrices add a level of complexity, such as how different matrices are combined with each other. However, MATLAB -- optimized for efficient operations on data formatted as a matrix -- provides simple syntax for operations on matrices, such as addition and concatenation.
Instructions
-
-
1
Create two matrices, as in this example filled with random data.
A = rand(50,50);
B = rand(50,50); -
2
Add the elements of the two matrices together using the "+" operator.
new_50by50_matrix = A+B;
-
-
3
Concatenate matrices by placing them together between brackets. A space or comma between matrix variable concatenates row-wise. A semicolon concatenates column-wise.
new_50by100_matrix = [A B];
new_50by100_matrix = [A, B];
new_100by50_matrix = [A; B]; -
4
Concatenate higher dimensional arrays along a certain dimension using the cat() function.
new_matrix = cat(dimension, a_matrix, another_matrix);
-
1
References
- Photo Credit Comstock Images/Comstock/Getty Images