- 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
How to Create a New Matrix from All Possible Row Combinations in MATLAB?
In MATLAB, a matrix is nothing but a 2D (two-dimensional) array of numbers arranged in multiple rows and columns. Matrix is the most elementary data structure in MATLAB programming which is utilized to store and manipulate data.
In this tutorial, we will learn how to create a new matrix from all possible row combinations in MATLAB. For this, we can use MATLAB's built-in function 'randperm'. The 'randperm' function will randomly select a row index and create a new matrix.
Algorithm
The step-by-step process to create a new matrix from all possible row combinations is described below −
Step (1) − Specify the number of rows (n) and columns (m) in the input matrix or matrices.
Step (2) − Create an input matrix of order n × m.
Step (3) − Specify the number of rows to be selected randomly.
Step (4) − Obtain the selected row indices from the matrix.
Step (5) − Randomly select the rows from the input matrix according to selected row indices.
Step (6) − Display the new created matrix.
Example (1): Create a Matrix by Randomly Selecting Rows From a Given Matrix
Now, let us consider a few MATLAB examples to practically understand how to create a new matrix from all possible row combinations in MATLAB.
% MATLAB program to create new matrix from all possible row combinations % Specify the rows and columns of the input matrix n = 4; % Row in given matrix m = 3; % Columns in given matrix % Generate a random matrix of order n x m mat = rand(n, m); % Specify the number of rows to be selected randomly x = 2; % Select row indices from the given matrix i = randperm(n, x); % Select rows randomly from the matrix new_mat = mat(i, :); % Display the input and new created matrices disp('The given input matrix is:'); disp(mat); disp('Randomly created new matrix is:'); disp(new_mat);
Output
The given input matrix is: 0.1656 0.6892 0.2290 0.6020 0.7482 0.9133 0.2630 0.4505 0.1524 0.6541 0.0838 0.8258 Randomly created new matrix is: 0.2630 0.4505 0.1524 0.6020 0.7482 0.9133
Explanation
This MATLAB code generates a new matrix where each row contains all possible combinations of rows from input matrix. The important thing about this code is that it will select all rows randomly, hence each time when we run the code, we will get a different result.
Example (2): Create a Matrix by Randomly Selecting All Rows From Given Matrix
% MATLAB Program to create a matrix by selecting all rows of given matrix randomly % Define the size of the input matrix n = 3; % Number of rows m = 4; % Number of columns % Create an input matrix mat = [1 2 3 4; 5 9 2 1; 4 3 8 7]; % Randomly select row indices from the input matrix i = randperm(n); % Select rows from the input matrix randomly new_mat = mat(i, :); % Display the input matrix and new created matrix disp('The given input matrix is:'); disp(mat); disp('Randomly created new matrix is:'); disp(new_mat);
Output
The given input matrix is: 1 2 3 4 5 9 2 1 4 3 8 7 Randomly created new matrix is: 1 2 3 4 5 9 2 1 4 3 8 7
Explanation
This MATLAB code accepts a given input matrix and create a new matrix by selecting all rows of the given matrix randomly. Therefore, the output matrix has the number of rows and columns equal to the given matrix.
Each time when we run the code, the code will produce a different resulting matrix created by randomly selecting rows from the input matrix.
Conclusion
In this tutorial, we explained how to create a new Matrix from all possible row combinations in MATLAB. MATLAB provides several ways of performing this task, but we have explained the easiest method in this tutorial.