- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating, Concatenating, and Expanding Matrices in MATLAB
In MATLAB, creating, concatenating, and expanding matrices are three basic matrix operations. Since, matrix is one of the essential data structures in MATLAB programming used to perform various data processing tasks. Hence, understanding the process of creating, concatenating, and expanding matrices is important to perform data organization and manipulation in MATLAB programming.
This article is meant for answering the following three questions related to MATLAB matrices −
How to create different types of matrices in MATLAB?
How to concatenate matrices?
How to expand matrices?
Let us discuss each operation one-by-one in detail.
Creating Matrix in MATLAB
In MATLAB, creating a matrix is nothing but a process of collecting and arranging data elements in rows and columns.
The process of creating a matrix starts with initialization and defining the data structure required.
In MATLAB, we can create several types of matrices, such as zero matrix, one matrix, random matrix, identity matrix, etc.
Let us see how to write MATLAB codes to create different types of matrices.
Create a Matrix of Given Data
If we are given a specific set of data, we can organize these data in the form of a matrix using square brackets.
If we want to arrange the element in a single row, then we use a comma or space in between the elements. If we want to arrange the data elements in multiple rows, then we use a semicolon to separate the rows.
Example
Let use an example in MATLAB programming to create a single row matrix and a matrix with multiple rows.
% MATLAB code to create a matrix % Create a single row matrix A = [15, 22, 5, 11, 7, 9]; % Create a matrix with multiple rows B = [15, 2, 9; 4, 3, 11; 7, 1, 9]; % Display the matrices disp('The single row matrix A:'); disp(A); disp('The multi row matrix B:'); disp(B);
Output
The single row matrix A: 15 22 5 11 7 9 The multi row matrix B: 15 2 9 4 3 11 7 1 9
Create a Zero Matrix
In MATLAB, there is a built-in function “zeros” used to create a matrix filled with zeros as all elements.
Syntax
mat = zeros(rows, columns);
Example
Let us see an example to create a zero matrix.
% Create a 3×3 zero matrix A = zeros(3); % Create a 2×4 zero matrix B = zeros(2, 4); % Display the results disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B);
Output
Matrix A: 0 0 0 0 0 0 0 0 0 Matrix B: 0 0 0 0 0 0 0 0
Create a Matrix With all Ones
In MATLAB, we can use the “ones()” function to generate a matrix having all elements 1.
Syntax
mat = ones(rows, columns);
Example
Let us take an example to create a matrix with all ones in MATLAB.
% Create a 4×4 ones matrix A = ones(4); % Create a 3×5 ones matrix B = ones(3, 5); % Display the results disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B);
Output
Matrix A: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Matrix B: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Create a Diagonal Matrix
In MATLAB, we have a built-in function “diag” that arranges the provided elements in the diagonal of the matrix.
Syntax
diagonal_matrix = diag(mat);
Example
Let us see how this function works with the help of an example.
% Create a sample matrix mat = [10, 20, 30, 40, 50]; % Create the diagonal matrix for “mat” d_mat = diag(mat); % Display the results disp('Sample matrix:'); disp(mat); disp('Diagonal matrix:'); disp(d_mat);
Output
Sample matrix: 10 20 30 40 50 Diagonal matrix: 10 0 0 0 0 0 20 0 0 0 0 0 30 0 0 0 0 0 40 0 0 0 0 0 50
Create an Identity Matrix
In MATLAB programming, we have a function “eye()” that can be used to create an identity matrix.
Syntax
mat = eye(n);
Here, n is the order of the identity matrix.
Example
Let us see an example to understand the creation of the identity matrix.
% Create an identity matrix of order of 3×3 A = eye(3); % Create an identity matrix of order of 5×5 B = eye(5); % Display the results disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B);
Output
Matrix A: 1 0 0 0 1 0 0 0 1 Matrix B: 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Create a Random Matrix
In MATLAB, we can use the “rand” function to create a random matrix in which each element’s value will be between 0 and 1.
Syntax
mat = rand(rows, columns);
Example
Let us take an example to create a random matrix in MATLAB.
% Create a random matrix of order of 3×3 A = rand(3, 3); % Create a random matrix of order of 3×4 B = rand(3, 4); % Display the results disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B);
Output
Matrix A: 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 Matrix B: 0.9649 0.9572 0.1419 0.7922 0.1576 0.4854 0.4218 0.9595 0.9706 0.8003 0.9157 0.6557
Each time when you will run this program, the value of elements will change randomly.
Concatenating Matrices in MATLAB
In MATLAB, concatenating matrices means combining two or more matrices together to create a single large matrix having elements of all the matrices.
Concatenating matrices is an important operation when we need to combine data from multiple sources.
In MATLAB, we can perform both horizontal concatenation and vertical concatenation.
Concatenate Matrices Horizontally
In MATLAB, we can combine or concatenate two matrices horizontally, provided that both matrices must have same number of rows.
Example
The following example demonstrates the process of concatenating two matrices horizontally or side-by-side.
% MATLAB code to concatenate matrices horizontally % Create sample matrices A = [10, 20; 30, 40]; B = [50, 60; 70, 80]; % Concatenate matrices A and B horizontally C = [A, B]; % Display the results disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B); disp('Concatenated Matrix C:'); disp(C);
Output
Matrix A: 10 20 30 40 Matrix B: 50 60 70 80 Concatenated Matrix C: 10 20 50 60 30 40 70 80
It can be seen that the matrix C is a combined matrix of matrices A and B side-by-side.
Concatenate Matrices Vertically
We can concatenate or combine two or more matrices vertically, provided that all the matrices must have the same number of columns.
Example
Let us see an example to understand the process of concatenating two matrices vertically.
% MATLAB code to concatenate matrices vertically % Create sample matrices A = [10, 20; 30, 40]; B = [50, 60; 70, 80]; % Concatenate matrices A and B vertically C = [A; B]; % Display the results disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B); disp('Concatenated Matrix C:'); disp(C);
Output
Matrix A: 10 20 30 40 Matrix B: 50 60 70 80 Concatenated Matrix C: 10 20 30 40 50 60 70 80
This example demonstrates the way of concatenating two matrices vertically.
Expanding Matrices in MATLAB
The process of increasing the size or order of a given matrix by adding additional columns or rows to it, is called expanding the matrix.
Expanding a matrix is a useful operation, when we want to change the shape of the matrix or store additional data to perform particular operations.
Expending Matrix by Adding Rows
We can expend an existing matrix vertically by adding a new row or rows to it.
Example
Let us take an example to understand the method of adding rows to a matrix to expand it.
% MATLAB code to add rows to a matrix % Create a sample matrix A = [10, 20; 30, 40]; % Display the matrix A before expanding disp('Original Matrix A:'); disp(A); % Specify new rows to be added N = [50, 60; 70, 80]; % Add new rows to expand the matrix A A = [A; N]; % Display matrix A after expansion disp('Expended Matrix A:'); disp(A);
Output
Original Matrix A: 10 20 30 40 Expended Matrix A: 10 20 30 40 50 60 70 80
Expanding Matrix by Adding Columns
We can also expand a matrix by adding new columns to it. The following example demonstrates how we can expand a matrix by adding new columns to an existing matrix.
Example
% MATLAB code to add columns to a matrix % Create a sample matrix A = [10, 20; 30, 40]; % Display the matrix A before expanding disp('Original Matrix A:'); disp(A); % Specify new column to be added N = [50; 60]; % Add new column to expand the matrix A A = [A, N]; % Display matrix A after expansion disp('Expended Matrix A:'); disp(A);
Output
Original Matrix A: 10 20 30 40 Expended Matrix A: 10 20 50 30 40 60
Conclusion
In conclusion, creating, concatenating, and expanding matrices are the fundamental operations that can be performed using MATLAB. These three matrix operations are essential in various fields such as engineering, technology, science, statistics, and more. The understanding of creating, concatenating, and expanding matrices are important to organize and manipulate the numerical data in various data analysis tasks. In this article, I have explained these three operations with the help of examples.