Matlab

How to Swap Elements in the Matrix in MATLAB?

A matrix is like a table made up of numbers. It’s a useful way to organize and work with data. For instance, you can use a matrix to keep track of students’ grades or measure the distances between different cities.

Sometimes, you might need to swap the elements in a matrix. For example, you might want to rearrange the order of the elements, or you might want to swap the elements in a specific row or column.

Methods for Swapping Elements in a Matrix

Multiple methods are there for swapping elements in a matrix in MATLAB. Some of them include:

Using a Temporary Variable

To swap elements in a matrix using the simplest method, you can follow these straightforward steps. First, create a temporary variable, which is like a temporary storage space. Then, take the value of the first element and store it in the temporary variable. After that, replace the value of the first element with the value of the second element. Lastly, assign the value stored in the temporary variable to the second element.

By doing this, the two elements have effectively switched places in the matrix. This approach allows you to exchange the values of elements.

Below MATLAB code swap the first and second elements of a matrix:

A = [1 2 3 4 5 6]

temp = A(1);

A(1) = A(2);

A(2) = temp;

Swapping Elements by Changing Elements of Rows and Columns

To swap elements of a matrix in MATLAB we simply need to specify the element’s position and assign the new position values to swap each defined element.

Example 1

The code swaps the first and second elements of the first column in matrix A, resulting in the updated matrix [300 200; 100 400].

A = [100 200

300 400]

 

% Swapping the first and second elements of the first column

A([1 2]) = A([2 1])

A picture containing text, screenshot, display, software Description automatically generated

Example 2

The code performs a swapping operation on a matrix called A. Specifically, it switches the positions of the second and third elements in the first column of the matrix, creating a temporary matrix in the process. Then, it further swaps the positions of the first and second elements in the second column of the temporary matrix.

A = [1 2 3

4 5 6

7 8 9]

 

% Swap the second and third elements of the first column

A([2 3]) = A([3 2])

% Swap the first and second elements of the second column of the newly created swapped matrix

A([4 5]) = A([5 4])

A screenshot of a computer Description automatically generated

Swapping Elements Using randperm() and size() Functions

In MATLAB, you can swap elements in a matrix using the randperm() and size() functions. Here’s a brief explanation of the method:

The size() function returns matrix size, i.e., the number of rows and columns.

The randperm() returns a random permutation of integers starting from 1 to the specified size of the matrix. This permutation will be used to swap the elements in the matrix.

By accessing the matrix elements using the generated permutation, you can swap their positions.

Example 1: Swapping Rows Randomly

The code randomly shuffles the rows of matrix A using the randperm() function with the size of A as a parameter, resulting in a randomly reordered matrix.

% row-wise swapping element

A = [9 8 7

6 5 4

3 2 1];

 

% randperm() function called with size() as parameter

random = A(randperm(size(A, 1)),:)

A screenshot of a computer Description automatically generated

Example 2: Swapping Column Randomly

The code randomly shuffles the columns of matrix A using the randperm() function with the size of A as a parameter, resulting in a randomly reordered matrix.

% column-wise swapping element

A = [1 2 3

4 5 6

7 8 9];

 

% randperm() function called with size() as parameter

random = A(:, randperm(size(A, 1)))

Conclusion

MATLAB provides several methods to swap elements in a matrix. The first method involves using a temporary variable to store and exchange values between elements, allowing for straightforward swapping. The second method utilizes indexing to directly assign new values to specific elements, enabling the swapping of rows and columns. The last method we covered here is using the randperm() and size() functions to randomly shuffle rows or columns in a matrix, providing a randomized reordering of the elements. With these techniques, MATLAB users can easily perform element swaps.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.