How to Create a New Matrix From All Possible Row Combinations in MATLAB? (original) (raw)

Last Updated : 28 Apr, 2025

A matrix represents a collection of numbers arranged in an order of rows and columns. A matrix encloses the elements in parentheses or brackets. We will learn how to create a new matrix from all possible row combinations of rows of a different matrix and display the combinations.

Procedure of Making a Matrix:

We can see the below examples to create a new matrix from all possible row combinations.

Syntax:

variable = randperm(parameter , parameter); %for taking random numbers

From the command randperm() it uses random numbers to make a matrix of row combinations.

Example:

Matlab `

% MATLAB code to Create a matrix randomly % picking rows from a Matrix % size of given matrix i = 5; j = 4 ; a = rand(i,j) ;

% pick rows from matrix a pick = 5 ;

% select rows randomly id = randperm(i,pick) ;
iwant = a(id,:) ; disp(iwant);

`

Output:

Explanation:

In the above example, we are declaring the number of rows, and columns, and declaring a variable that stores random numbers and makes a matrix. In the above, we are taking random numbers in the matrix but we can also declare typically.

Create a Matrix by Selecting All Rows Randomly:

Now we are creating a matrix by selecting all rows randomly. In this, we will declare a number of rows and columns and we create columns through another.

Below are the examples through which we can understand.

Syntax:

variable1 = rand(rows,columns); %make a matrix of nth rows and m columns.

variable2 = randperm(nth_rows); %select rows randomly

Explanation:- rand() command makes a matrix of rows and columns and randomly stores a number.

Example:

Matlab `

% MATLAB code for create a matrix % randomly picking rows from a Matrix % size of given matrix i = 5; j = 6 ;
A = rand(i,j) ;

% select rwos randomly indx = randperm(i) ;
iwant = A(indx,:) ; disp(iwant);

`

Output:

Explanation:

In this, we are declaring i =5, j=6 ( number of rows and columns ) and we create columns through another. This means we are creating a matrix of possible row combinations and at last, we will display it on our screen.

Create a 3D Matrix Selecting Rows Randomly:

In this, we will create a 3D matrix by selecting randomly a row and we will display the new matrix on the screen. Below is a suitable example to understand.

Syntax:

var = perms(rows); % this will arrange all rows randomly

perms() command will arrange all rows randomly and the rand() command will take random numbers. For implementation check out the below example.

Example:

Matlab `

% MATLAB code for Create a matrix % randomly picking rows from a Matrix % size of given matrix i = 3; j = 5 ; Ax = rand(i,j) ;

% pick rows from matrix As % arrange all rows randomly indx = perms(i) ;
N = size(indx,1) ; iwant = Ax(i,j,N) ; for k = 1:N iwant(:,:,k) = Ax(indx(k,:),:) ; end disp(iwant);

`

Output:

Explanation:

In this we are declaring i =3, j=5 ( number of rows and columns ) and we create columns through another. This means we are creating a matrix of possible row combinations and at last, we will display it on our screen.