Open In App

Extract Subsets of Consecutive Entries in a Matrix in MATLAB

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we shall discuss how to obtain subsets of consecutive entries in a matrix. Now, we shall some of the basic concepts and then, put them together to get a result. Let us see the same.

Subset from Consecutive Rows

First, we shall see how to extract subsets from consecutive rows. What we need to do is take input from the user, regarding the number of rows they want in the subset and then, shall find a random row number that leaves at least the required number of rows from that row number, and then slice the matrix. See the following code example to understand it.

Example 1:

Matlab




% MATLAB Code
mat = magic(10);
rownum = 5;
sub = selection(mat,rownum);
sub            
  
%function
function selected = selection(mat, p)
     
   %generating a random row number
   i = randi(size(mat,1)-p+1);
     
   %generating a subset by slicing the matrix
   selected = mat(i:i+p-1, :);
end


In this code, we create a magic square matrix and take the required number of rows in our subset matrix to be 5. Then, we call the selection function to extract a subset. In the selection function, we first find a random row number, which has at least p -1 rows after it for slicing, using the Randi function (which returns a random integer). Then, we create a sub-array by using array slicing methods and thus, extract a subset of the matrix mat.

Output:

 

As can be seen above, we extracted a subset from rows 2 to 6, randomly. This code will produce a different subset on every execution because of using the random integer generator.

Subset from Consecutive Columns

Now, we shall do the same for selecting random columns. There is no change except in the selection function. In the above-mentioned code of the selection function, we need to change the dimension in the size function from 1 to 2 (from rows to columns) and index the same from ( i : i+p-1, 🙂 to ( : , i : i+p-1). 

Example 2:

Matlab




% MATLAB Code
mat = magic(10);
colnum = 5;
sub = selection(mat,colnum);
sub;
  
%function
function selected = selection(mat, q)
  
   %generating a random row number
   i = randi(size(mat,2)-q+1);
  
   %generating a subset by slicing the matrix
   selected = mat(:, i:i+q-1);
end


The output will contain 5 random-consecutive columns from the matrix mat.

Output:

 

Subset from Consecutive Rows and Columns

So far, we have explained how to get a set number of consecutive rows and columns individually. Now, to get a subset with a user-defined number of rows and columns, we only need to combine the above two code snippets. See the complete code below for an example.

Example 3:

Matlab




% MATLAB Code
mat = magic(10);
rownum = 5;
colnum = 5;
sub = selection(mat,rownum, colnum);
sub;
  
  
function selected = selection(mat, p, q)
   %generating a random row number
   i = randi(size(mat,1)-p+1);
    
  %generating a random row number
   j = randi(size(mat,2)-q+1);
    
  %generating a subset by slicing the matrix
   selected = mat(i:i+p-1, j:j+q-1);
end


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads