How to Select Random Rows from a Matrix in MATLAB? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like 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. Comment More infoAdvertise with us Next Article How to Select Random Rows from a Matrix in MATLAB? O owl0223 Follow Improve Article Tags : Software Engineering MATLAB-matrix Similar Reads 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" 3 min read How to Remove Nan Values from a Matrix in MATLAB? Removal of Nan Values from a Matrix.There are multiple methods by which we can remove Nan values from a specified matrix:. Method 1: By using rmmissing( ) This function is used to remove missing entries or Nan values from a specified matrix. Syntaxrmmissing(A) Parameters: This function accepts a pa 2 min read How to Randomly Shuffle Columns in MATLAB in Matrix? In this article, we are going to discuss the "random shuffling of columns in a Matrix " with the help of size() and randperm() function. The size() function is used to return the size of each dimension of the specified array "X" or the size of the specified matrix "X". Using Size() and randperm() 1 3 min read How to Select Random Row in MySQL In database operations, selecting random rows from a table is a common requirement for various applications, such as gaming, content recommendation, and statistical sampling. In this article, we learn different methods for selecting random rows in MySQL. We'll understand various approaches, includin 6 min read How to Select 10 Random Rows from 600K Rows Fast in MySQL? Selecting random rows simultaneously from a database is a common task in SQL especially when handling large datasets. Selecting multiple rows is useful for sampling data or generating random subsets for analysis. In MySQL, this can be achieved using various methods, each has its advantages. In this 4 min read Like