How to swap elements in the matrix in MATLAB?

24 Jan, 2022
In this article, we will see the swapping of elements into a matrix in MATLAB. Different methods are illustrated below:
 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 swappedA = [5  10     15 20]      % Swapping the first and second elements of the first columnA([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 columnA([2 3]) = A([3 2]) % Swapping the first and second elements of the second column% of the above swapped matrixA([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
 In this approach, we are using the combination of randperm() and size() functions. 
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.
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 arrayA = [1 2 3     4 5 6     7 8 9]; % Calling the randperm() function with% size() as its parameterrandom = 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 arrayA = [1 2 3     4 5 6     7 8 9]; % Calling the randperm() function with% size() as its parameterrandom = A(:, randperm(size(A, 1)))
Output:
random =
  3   1   2
  6   4   5
  9   7   8
prachisoda1234
saurabh1990aror
MATLAB Matrix-Programs
MATLAB-programs
MATLAB
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Convert Three Channels of Colored Image into Grayscale Image in MATLAB?
How to Solve Histogram Equalization Numerical Problem in MATLAB?
Adaptive Histogram Equalization in Image Processing Using MATLAB
MRI Image Segmentation in MATLAB
How to detect duplicate values and its indices within an array in MATLAB?
Double Integral in MATLAB
Classes and Object in MATLAB
How to remove space in a string in MATLAB?
How to Normalize a Histogram in MATLAB?
Forward and Inverse Fourier Transform of an Image in MATLAB