Swapping Two Elements in Each Row of a Matrix Without Loop in MATLAB


Read this tutorial to learn the method of swapping two elements in each row of a matrix without using loop in MATLAB. MATLAB is a powerful to manipulate matrices. We can perform various operations on matrices using MATLAB.

In MATLAB, we can swap any two elements in each row of a matrix using the "matrix indexing method". This method of swapping two elements in rows of a matrix is explained below with the help of an example.

How to Swap Two Elements in Each Row of Matrix without using Loop

In MATLAB, we can use the matrix indexing to swap two elements in each row of a matrix and without using any loop. It is the simplest method of swapping two elements in each row of a matrix.

The step wise explanation of this method is given below −

  • Step 1 − Create a matrix.

  • Step 2 − Display the original matrix

  • Step 3 − Determine the size of the matrix.

  • Step 4 − Swap any two elements in each row of the matrix using the matrix indexing.

  • Step 5 − Display the new matrix with swapped elements.

Example

Let us understand this method with the help of an example in MATLAB.

% MATLAB code for swapping two elements in each row of a matrix using matrix indexing
% Create a sample matrix
A = [4 2 5 1; 5 8 2 3; 7 4 9 6];

% Display original matrix
disp('Original Matrix:');
disp(A);

% Determine the size of the matrix
[m, n] = size(A);

% Swap two elements in each row of matrix
A(:, [1, n]) = A(:, [n, 1]);	% Swapping first and last elements
disp('Swapping first and last elements in each row:');
disp(A);

Output

It will produce the following output −

Original Matrix:
     4     2     5     1
     5     8     2     3
     7     4     9     6
Swapping first and last elements in each row:
     1     2     5     4
     3     8     2     5
     6     4     9     7

In this example, we have swapped the first and last element of each row in the matrix.

Similarity, if we want to swapping any other two elements, we alter the fourth step code line of the program as follows −

Swapping first and third elements in each row

A(:, [1, 3]) = A(:, [3, 1]);

It will produce the following output

Original Matrix:
     4     2     5     1
     5     8     2     3
     7     4     9     6
Swapping first and last elements in each row:
     5     2     4     1
     2     8     5     3
     9     4     7     6

Swapping second and last elements in each row

A(:, [2, n]) = A(:, [n, 2]);

It will produce the following output

Original Matrix:
     4     2     5     1
     5     8     2     3
     7     4     9     6
Swapping first and last elements in each row:
     4     1     5     2
     5     3     2     8
     7     6     9     4

This is how we can swap any two elements in each row of a matrix using the matrix indexing method.

Conclusion

In conclusion, we can swap any two elements in each row of a matrix in MATLAB. The swapping of two elements of a matrix involves the change in the position of the elements. To swap any two elements in a matrix, we just need to interchange their indices. This process is called "matrix indexing", which we discussed in detail with an example to demonstrate the code implementation for swapping of two elements in each row of a matrix without using a loop.

Updated on: 26-Oct-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements