# Swap Rows and Columns in MATLAB

This tutorial demonstrates how to swap rows and columns in MATLAB.

MATLAB has different methods to swap rows and columns with each other. This tutorial includes different methods to swap rows and columns in MATLAB.

## Swap Rows and Columns With Base MATLAB

Base MATLAB has a simple functionality that can be used to swap rows with rows and columns with columns. Let’s try an example to swap rows with rows.

``````% Random 4x6 matrix
A = rand(4,6)

%Swap 1st row with the fourth.
A([4 1],:) = A([1 4],:)
``````

The code above creates a random `4x6` matrix and swaps the rows 1st with the 4th. See output: As we can see, row 1st is swapped with row 4th. Now let’s try the same example for the columns.

``````% Random 4x6 matrix
A = rand(4,6)

%Swap 1st column with the sixth.
A(:,[1 6]) = A(:,[6 1])
``````

You might think the code is similar, but there is a slight difference. For rows, we use the expression `A([4 1],:)`, and for columns, we use the comma and colon symbol at the start like `A(:,[1 6])`.

See the output for this code. ## Swap Rows and Columns With a User-Defined Function in MATLAB

We can also create a user-defined function using the base MATLAB functionality, which can be used for both rows and columns. Here is the function.

`swap.m`:

``````function matrix = swap(matrix,dimension,a,b)

if dimension == 1
row_swap = matrix(a,:);
matrix(a,:) = matrix(b,:);
matrix(b,:) = row_swap;
elseif dimension == 2
col_swap = matrix(:,a);
matrix(:,a) = matrix(:,b);
matrix(:,b) = col_swap;
end
``````

The above function will take four parameters, first, the matrix; second, the dimension where 1 means row and 2 means columns, and two elements to swap with each other, either rows or columns.

Let’s try to swap rows with this function.

``````% Random 4x6 matrix
A = rand(4,6)

% Swap rows using the swap function
swap(A,1,1,4)
``````

The code above will swap the first row with the fourth. See output: To swap the columns, we need to change the dimension to 2. See example:

``````% Random 4x6 matrix
A = rand(4,6)

% Swap rows using the swap function
swap(A,2,1,6)
``````

The code above will swap the column first with the sixth using the `swap` function. See output: ## Swap Rows and Columns Using a Combination of `randperm()` and `size()` Functions in MATLAB

The `randperm()` method is used for the random permutation of integers for a specified matrix. It takes only one parameter, which is the matrix.

The `size()` method returns the size for each dimension of an array or the size of a specified matrix. This method can be used in the following different ways.

1. `size(X)` - Returns the size of each dimension of an array.
2. `[m,n] = size(X)` - Returns the size of the specified matrix in two variables.
3. `size(X,dim)` - Returns the size of `X` dimension by scalar `dim`.
4. `[d1,d2,d3,…,dn] = size(X)` - Return the sizes for the `n` dimensions for an array in separate variables.

Where `X` is the array, matrix, or dimension and `dim` is the scalar value for the dimension. The disadvantage of this method is that it randomly swaps the rows or columns.

Let’s try to swap rows using this method.

``````% Random 4x4 matrix
A = rand(4,4)

% Calling the randperm() and size() methods
% to swap rows
Swapped = A(randperm(size(A, 1)),:)
``````

The code above will randomly swap a row with another. See output:

``````A =

0.4574    0.6377    0.2891    0.2548
0.8754    0.9577    0.6718    0.2240
0.5181    0.2407    0.6951    0.6678
0.9436    0.6761    0.0680    0.8444

Swapped =

0.9436    0.6761    0.0680    0.8444
0.4574    0.6377    0.2891    0.2548
0.5181    0.2407    0.6951    0.6678
0.8754    0.9577    0.6718    0.2240
``````

Similarly to swap columns, here is the example.

``````% Random 4x4 matrix
A = rand(4,4)

% Calling the randperm() and size() methods
% to swap rows
Swapped = A(:, randperm(size(A, 1)))
``````

The code above will randomly swap the columns for the given matrix. See output:

``````A =

0.6022    0.4624    0.3225    0.1759
0.3868    0.4243    0.7847    0.7218
0.9160    0.4609    0.4714    0.4735
0.0012    0.7702    0.0358    0.1527

Swapped =

0.3225    0.6022    0.4624    0.1759
0.7847    0.3868    0.4243    0.7218
0.4714    0.9160    0.4609    0.4735
0.0358    0.0012    0.7702    0.1527
``````