Extract Subsets of Consecutive Entries in a Matrix in MATLAB


MATLAB is a powerful tool to manipulate matrices. In this article, we will explore how to extract subsets of consecutive entries in a matrix using MATLAB. But before that let's first get an overview of subsets of consecutive entries in a matrix.

In a matrix, a set of elements that located in either rows or columns, or both in a sequence is referred to as a subset of consecutive entries in the matrix. The subsets of consecutive entries in a matrix can be extracted by specifying a range of columns and rows. The subsets of consecutive entries in a matrix are used to analyze and manipulate a specific part of a large matrix.

Now, let us discuss how to extract subsets of consecutive entries in a matrix using MATLAB.

In MATLAB, there can be following three cases in extracting subsets of consecutive entries in a matrix:

  • Extract subsets from consecutive rows in a matrix

  • Extract subsets from consecutive columns in a matrix

  • Extract subsets from consecutive rows and columns together in a matrix

Let's discuss each case in detail with the help of examples.

Extract Subsets from Consecutive Rows in a Matrix

In MATLAB, we can extract subsets from consecutive rows in a matrix by using the array indexing.

Example

Let us understand this concept with the help of an example program.

% MATLAB program to extract subsets from consecutive rows
% Create a sample matrix
M = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20];

% Specify the range of consecutive rows to extract
S = 2;		% Start row
E = 4;		% End row

% Extract the subsets of consecutive rows
sub = M(S:E, :);	% Array indexing

% Display the original matrix and the extracted subset
disp('Original matrix is:');
disp(M);
disp('Extracted subsets of consecutive rows is:');
disp(sub);

Output

Original matrix is:
     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

Extracted subsets of consecutive rows is:
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

Code Explanation

In this example, we start by creating a matrix 'M'. Then, we specify the number of consecutive rows to be extracted, as 'S' is the starting row and 'E' is the ending row. After that we use the array indexing to extract the specified consecutive rows from the matrix.

Finally, we display the original matrix and the extracted matrix using the 'disp' function.

Extract Subsets from Consecutive Columns in a Matrix

Similar to consecutive rows, we can also use the array indexing to extract subsets from consecutive columns in a matrix.

Example

The following MATLAB program shows how to extract subsets from consecutive columns using MATLAB.

% MATLAB program to extract subsets from consecutive columns
% Create a sample matrix
M = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20];

% Specify the range of consecutive columns to extract
S = 2;		% Start column
E = 4;		% End column

% Extract the subsets of consecutive columns
sub = M(:, S:E);	% Array indexing

% Display the original matrix and the extracted subset
disp('Original matrix is:');
disp(M);
disp('Extracted subsets of consecutive columns is:');
disp(sub);

Output

Original matrix is:
     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

Extracted subsets of consecutive columns is:
     2     3     4
     7     8     9
    12    13    14
    17    18    19

Code Explanation

The implementation and execution of this MATLAB program is the same as that of the above program. This MATLAB code uses array indexing to extract subsets of consecutive columns and store the result in a variable 'sub'.

Extract Subsets from Consecutive Rows and Columns in a Matrix

In MATLAB, to extract the subsets of consecutive rows and columns in a matrix, we use both row and column indexing together.

Example

The following MATLAB program demonstrates how to extract a rectangular subset of consecutive rows and columns in a matrix.

% MATLAB program to extract subsets from consecutive rows and columns
% Create a sample matrix
M = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20];

% Specify the range of consecutive rows to extract
S_R = 1;		% Start row
E_R = 3;		% End row

% Specify the range of consecutive columns to extract
S_C = 2;		% Start column
E_C = 4;		% End column

% Extract the subsets of consecutive rows and columns using array indexing
sub = M(S_R:E_R, S_C:E_C);

% Display the original matrix and the extracted subset
disp('Original matrix is:');
disp(M);
disp('Extracted subsets of consecutive rows and columns is:');
disp(sub);

Output

Original matrix is:
     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

Extracted subsets of consecutive rows and columns is:
     2     3     4
     7     8     9
    12    13    14

Code Explanation

In this MATLAB code, we have used the array indexing for both rows and columns together to extract the subsets of consecutive rows and columns from the matrix. This code gives a rectangular subset as a result.

Conclusion

In this tutorial, we demonstrated how to extract consecutive rows or columns or both from a matrix with the help of example programs. In MATLAB, this function can be easily performed by using the concept of array indexing.

Updated on: 07-Sep-2023

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements