Open In App

How to swap elements in the matrix in MATLAB?

Last Updated : 24 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the swapping of elements into a matrix in MATLAB. Different methods are illustrated below:

Method 1: By changing elements of rows and columns

 In this method, we are simply changing the elements of particular rows and columns in the specified rows and columns respectively.

Example 1:

Matlab




% MATLAB code for 2*2 matrix. its first and
% second elements of the first column are being swapped
A = [5  10
     15 20]
      
% Swapping the first and second elements of the first column
A([1 2]) = A([2 1])


Output:

A =
   5   10
  15   20
A =
  15   10
   5   20

Example 2:

Matlab




% MATLAB code for 3*3 matrix. The second and third elements of the first
% column are being swapped. And later, the first and second elements
% of the second column of the swapped matrix are swapped again.
A = [5  10 15
     20 25 30
     35 40 45]
      
% Swapping the second and third elements of the first column
A([2 3]) = A([3 2])
 
% Swapping the first and second elements of the second column
% of the above swapped matrix
A([4 5]) = A([5 4])


Output:

A =
   5   10   15
  20   25   30
  35   40   45
A =
   5   10   15
  35   25   30
  20   40   45
A =
   5   25   15
  35   10   30
  20   40   45

 Method  2: By using randperm() and size() functions

 In this approach, we are using the combination of randperm() and size() functions. 

 randperm()

The randperm() function is used for the random permutation of integers of the specified matrix.

Syntax: 

randperm(A)

Parameters: This function accepts a parameter.

  • A: This is the specified matrix.

size()

The size() function is used to return the size of each dimension of the specified array “X” or the size of the specified matrix “X”.

Syntax:

size(X)

[m,n] = size(X)

size(X,dim)

[d1,d2,d3,…,dn] = size(X)

Here,

size(X) returns the size of each dimension of the specified array “X” in a vector d with ndims(X) elements.

[m,n] = size(X) returns the size of the specified matrix “X” in the separate variables m and n.

size(X,dim) returns the size of the dimension of “X” specified by scalar dim.

[d1,d2,d3,…,dn] = size(X) returns the sizes of the first n dimensions of the specified array “X” in separate variables.

Parameters: This function accepts two parameters, which are illustrated below:

  • X: It is the specified array or matrix or dimension.
  • dim: It is the scalar value for the specified dimension “X”

Example 1: 

Matlab




% MATLAB code for swapping element
% of the array row-wise
% Initializing an array
A = [1 2 3
     4 5 6
     7 8 9];
 
% Calling the randperm() function with
% size() as its parameter
random = A(randperm(size(A, 1)),:)


Output:

random =
  7   8   9
  1   2   3
  4   5   6

Example 2:

Matlab




% MATLAB code for swapping elements
% of the array column-wise
% Initializing an array
A = [1 2 3
     4 5 6
     7 8 9];
 
% Calling the randperm() function with
% size() as its parameter
random = A(:, randperm(size(A, 1)))


Output:

random =
  3   1   2
  6   4   5
  9   7   8


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads