
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swap Elements in a Matrix Using MATLAB
A matrix is a structure which is used to store and work with numeric data. Each value stored in a matrix is referred to as an element. Sometimes, we need to swap or change the position of these elements within the matrix. We can use MATLAB to perform this task i.e., swapping the elements of the matrix. In this tutorial, I will explain how we can use MATLAB to swap the elements in a matrix.
Swap Elements in a Matrix
MATLAB is a powerful tool that can be used to manipulate matrices. It provides various methods to swap the elements of a matrix. Some commonly used methods are listed and described below
Swap elements of a matrix using "size()" and "randperm()" functions.
Swap elements of a matrix using a temporary variable.
Swap elements of a matrix by interchanging elements of rows and columns.
Let us discuss all these methods of swapping elements of a matrix in detail.
Swap Elements of a Matrix using "size()" and "randperm()" Functions
In MATLAB, the "size" and "randperm" are built-in functions. Where, the "size" function used to determine the dimensions i.e., number of rows and columns, in the matrix. Whereas, the "randperm" function is used to compute the random permutation of input values.
Hence, we can use the "randperm" function to calculate the random permutation of row and column indices to swap the rows and columns of the matrix.
The syntax formats of these functions are given below
mat_size = size(mat, dim);
where, "dim = 1" to determine the number of rows in the matrix and "dim = 2" to determine the number of columns in the matrix.
To swap the rows of the matrix
rand_row_indx = randperm(mat_size, :);
To swap the columns of the matrix
rand_col_indx = randperm(:, mat_size);
The steps involved in swapping the elements of a matrix using the "size" and "randperm" functions are given below
Step 1 Create a matrix.
Step 2 Determine the number of rows or columns in the matrix using the "size" function.
Step 3 Calculate the random permutation of row indices or column indices using the "randperm" function.
Step 4 Use these randomly generated indices to swap the elements of the matrix.
Step 5 Display the results.
Example 1
Let us understand the implementation of these steps with the help of an example.
% MATLAB program to swap elements of a matrix using size and randperm functions % Create a sample matrix mat = [1 5 3 4 6; 4 8 2 4 3; 7 8 1 4 6; 2 3 6 7 3]; % Determine the rows and columns in the matrix rows = size(mat, 1); cols = size(mat, 2); % Calculate the random permutation of row and column indices random_rows = randperm(rows); % Random row indices random_cols = randperm(cols); % Random column indices % Use random row and column indices to swap elements in the matrix swapped_rows = mat(random_rows, :); swapped_cols = mat(:, random_cols); % Display the original and swapped matrices disp('Original Matrix:'); disp(mat); disp('Swapped Rows Matrix:'); disp(swapped_rows); disp('Swapped Columns Matrix:'); disp(swapped_cols);
Output
When you run this code, it will produce the following output
Original Matrix: 1 5 3 4 6 4 8 2 4 3 7 8 1 4 6 2 3 6 7 3 Swapped Rows Matrix: 7 8 1 4 6 1 5 3 4 6 4 8 2 4 3 2 3 6 7 3 Swapped Columns Matrix: 5 3 4 1 6 8 2 4 4 3 8 1 4 7 6 3 6 7 2 3
This example demonstrates the swapping of elements in a matrix using the "size" and "randperm" functions.
Swap Elements of a Matrix using a Temporary Variable
In MATLAB, the swapping of elements in a matrix by using a temporary variable is the simplest and most straightforward method. In this method, we use a temporary variable to store one element of the matrix, and then this element is replaced by a second element.
Here is the step-by-step process to swap elements in a matrix using the temporary variable method is explained below
Step 1 Create a matrix.
Step 2 Specify the indices of elements to be swapped in the matrix.
Step 3 Create a temporary variable to swap the elements of the matrix.
Step 4 Display the results.
Example 2
Let us understand this method of swapping elements in a matrix with the help of an example.
% MATLAB program to swap elements of a matrix using temporary variable % Create a sample matrix mat = [1 3 5 4 7; 4 2 7 4 9; 7 4 5 3 6; 1 3 2 9 3]; % Display the original matrix disp('Original Matrix:'); disp(mat); % Specify the indices of elements to be swapped row1 = 1; col1 = 2; row2 = 3; col2 = 5; % Create a temporary variable to swap the elements temp = mat(row1, col1); % Storing first element in temporary variable % Replacing first element with second element mat(row1, col1) = mat(row2, col2); % Assigning value of temporary variable to second element mat(row2, col2) = temp; % Display the matrix with swapped elements disp('Matrix after Swapping of Elements:'); disp(mat);
Output
It will produce the following output
Original Matrix: 1 3 5 4 7 4 2 7 4 9 7 4 5 3 6 1 3 2 9 3 Matrix after Swapping of Elements: 1 6 5 4 7 4 2 7 4 9 7 4 5 3 3 1 3 2 9 3
This is how we can use a temporary variable to swap the elements in a matrix in MATLAB.
Swap Elements of a Matrix by Interchanging Elements of Rows and Columns
In this method, we swap the elements in the matrix by changing the indices or positions of the rows and columns of the matrix.
The swapping of elements in a matrix by interchanging rows and columns is done as per the following steps
Step 1 Create a matrix.
Step 2 Specify the position of the elements and assign then a new position to swap them.
Step 3 Display the result.
Example 3
The following example demonstrates the implementation of these steps in MATLAB programming.
% MATLAB code to swap elements in a matrix by changing rows and columns % Create a sample matrix mat = [1 5 4 6; 7 1 5 6; 4 2 3 7]; % Display the original matrix disp('Original Matrix:'); disp(mat); % Swap the elements by changing rows and columns mat([1 2 3 4]) = mat([3 1 4 2]); % Display the matrix after swapping the elements disp('Matrix after Swapping:'); disp(mat);
Output
It will produce the following output
Original Matrix: 1 5 4 6 7 1 5 6 4 2 3 7 Matrix after Swapping: 4 7 4 6 1 1 5 6 5 2 3 7
This example demonstrates the swapping of elements in a matrix by changing rows and columns.
Conclusion
In conclusion, swapping of elements in a matrix is an operation that changes the position of the elements within the matrix. In this article, I explained the three most commonly used methods to swap elements in a matrix.