Open In App

How to Select Random Rows from a Matrix in MATLAB?

Last Updated : 09 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array.

 Syntax:

matrix_name(i,j)
where, i is the row number, and  J is the column number which is to be indexed.

Example 1: 

Matlab




% MATLAB code for select 
% random matrix elements 
mat = magic(5);
mat(2,5)


Output:

 

Selecting Random Rows From a Matrix:

We can use the randi() function to select random rows from a given matrix. 

Syntax:

randi(n)

It gets random integers from the range 1 to n. We will select two random rows from a magic square.

Example 2:

Matlab




% MATLAB code for select 
% random matrix elements in range
  
a = magic(5);
  
% Selecting random row 1
rand_row1 = a(randi(5),:);
  
% Selecting random row 2
rand_row2 = a(randi(5),:);


Output:

 

We can visually verify that the above program indexed the 5th and 4th rows of the magic square.

In case the user needs the row number of the random row selected, it can be done easily by storing the Randi value in a variable.

Example 3:

Matlab




% MATLAB code for select 
% random matrix raw elements 
a = magic(5);
  
% Selecting random row 1
rr1 = randi(5);
rand_row1 = a(rr1,:);
  
% Selecting random row 2
rr2 = randi(5);
rand_row2 = a(rr2,:);


Output:

 

This program will store the first random row number in rr1 and the second random row number in rr2. 



Similar Reads

How to Permute the Rows and Columns in a Matrix on MATLAB?
In this article, we will discuss how to find the permutation of the rows and columns in a Matrix with the help of multiple approaches Method 1In this approach, we are simply permuting the rows and columns of the matrix in the specified format of rows and columns respectively. For column permutation, we take an example of a 3*3 matrix being permuted
4 min read
How Can Be Randomly Shuffle Rows in MATLAB Matrix?
In this article, we are going to discuss the "random shuffling of rows in a Matrix " which can be done using size() and randperm() function. Functions Used 1) size(): The size() function is used to return the sizes of each dimension of the specified array "X" or the size of the specified matrix "X". Syntax:size(X) [m,n] = size(X) size(X,dim) [d1,d2
3 min read
How to Perform Random Pseudo Coloring in Grayscale Image Using MATLAB?
Pseudo Coloring is one of the attractive categories in image processing. It is used to make old black and white images or videos colorful. Pseudo Coloring techniques are used for analysis identifying color surfaces of the sample image and adaptive modeling of histogram black and white image. Selecting different values in the layers R, G, B is the m
3 min read
Random Numbers in MATLAB
Random numbers, as the name suggests, are numbers chosen randomly from a set of numbers. In practical application, classical computers cannot create truly random numbers as they are developed on binary logic thus, they require some sort of algorithm to generate random numbers, this type of random number is called a pseudorandom number. They are ran
3 min read
How To Export a Matrix as a CSV File in MATLAB?
A CSV file - Comma Separated File, as the name suggests, is a file that stores data delimited by a ' , '. In MATLAB, there is a simple way of exporting a matrix to a csv file. The writematrix() function is used to write a matrix into any desired file type. The syntax is writematrix(<matrix_name>, "filename.extension"); This will create a new
2 min read
Page-wise matrix multiplication in MATLAB
Page-wise matrix multiplication is multiplying two N-D arrays along each dimension or page of two arrays. Matlab allows users to calculate page-wise multiplication using pagemtimes(). Different syntax of pagemtimes() method are: Z = pagemtimes(X,Y)Z = pagemtimes(X,transpX,Y,transpY)Z = pagemtimes(X,Y)It returns the page-wise multiplication of N-D a
2 min read
Matlab - Matrix
A Matrix is a two-dimensional array of elements. In MATLAB, the matrix is created by assigning the array elements that are delimited by spaces or commas and using semicolons to mark the end of each row. Now let's have a glance at some examples to understand it better. Syntax: a = [elements; elements] Example: Creating a Matrix C/C++ Code % MATLAB p
4 min read
How to swap elements in the matrix in MATLAB?
In this article, we will see the swapping of elements into a matrix in MATLAB. Different methods are illustrated below: Method 1: By changing elements of rows and columns In this method, we are simply changing the elements of particular rows and columns in the specified rows and columns respectively. Example 1: C/C++ Code % MATLAB code for 2*2 matr
3 min read
Find inverse of matrix in MATLAB
Inverse function in MATLAB is used to find the inverse of a matrix. Suppose A is a matrix and B is the inverse of a then A*B will be an identity matrix. This function computes the inverse of a square matrix. This is used while solving linear equations. We can compute the inverse of a matrix by passing it to inv(). Syntax: inv(A) Parameters: It take
2 min read
Reduced Row Echelon Form (rref) Matrix in MATLAB
Reduced Row Echelon Form of a matrix is used to find the rank of a matrix and further allows to solve a system of linear equations. A matrix is in Row Echelon form if All rows consisting of only zeroes are at the bottom.The first nonzero element of a nonzero row is always strictly to the right of the first nonzero element of the row above it. Examp
3 min read
Article Tags :