Swapping Rows in MATLAB

This detailed article, intended for world class Python engineers and computer vision experts, aims to provide a thorough understanding of how to swap rows in MATLAB. Alongside explaining its uses, we …

Updated November 29, 2023

Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

This detailed article, intended for world class Python engineers and computer vision experts, aims to provide a thorough understanding of how to swap rows in MATLAB. Alongside explaining its uses, we will delve into various techniques and examples, while highlighting the importance of the knowledge gained from this skill. We believe that by mastering these concepts, you can greatly enhance your ability to solve complex data science problems efficiently.


MATLAB is a powerful programming language primarily designed for technical computing and mathematical problem-solving. It is used extensively across various fields such as engineering, physics, computer vision, finance, and more. As Python engineers and computer vision experts, you may sometimes need to manipulate arrays in MATLAB to perform specific tasks like data preprocessing or model training.

In this context, swapping rows in a matrix is an essential operation that comes into play when you want to rearrange data, analyze different scenarios, or experiment with different configurations. This article will help guide you through the process of row interchanging in MATLAB and showcase its usage in various situations.

The Basics of Swapping Rows in MATLAB

Before diving into the details of swapping rows in MATLAB, let’s first understand the fundamental principles of MATLAB arrays, specifically matrices. A matrix is a two-dimensional array containing rows and columns of elements. In MATLAB, these matrices can be manipulated using various built-in functions for reshaping, transposition, sorting, etc.

Swapping rows involves rearranging the position of rows within a matrix. To demonstrate the concept, let’s use a simple example: Assume we have a matrix A with three rows and two columns containing the elements as shown below:

A = [[1 2] [3 4] [5 6]]

In this case, swapping row 1 with row 2 would result in the following new arrangement of data:

B = [[3 4] [1 2] [5 6]]

Swapping can be performed manually by copying elements from one position to another or using programming techniques. In MATLAB, there are several built-in functions that facilitate row interchanging for various purposes. We will now discuss some of these techniques and explore their usage in different scenarios.

  1. Using the ‘flipud’ function The ‘flipud’ function is used to flip a matrix vertically along its diagonal, which means it swaps the upper half of rows with the lower half. In other words, row n becomes row (number_of_rows - n + 1) after using ‘flipud’. Let’s apply this method to our initial example:

A = [[1 2] [3 4] [5 6]] B = flipud(A)

We get the following output:

B = [[3 4] [1 2] [5 6]]

By using ‘flipud’, we have effectively swapped rows of our matrix A, as desired. However, this method might not always be suitable, especially if the matrix contains negative elements or data dependencies that make row reversal unfit for analysis purposes.

  1. Using the ‘rot90’ function The ‘rot90’ function is a versatile tool used to rotate MATLAB arrays by 90 degrees in either direction (clockwise or anti-clockwise). By passing an appropriate argument to this function, it can be employed as a row interchanging technique. For instance, consider our original matrix A:

A = [[1 2] [3 4] [5 6]]

To swap rows 1 and 2, we need to rotate A by 90 degrees clockwise (since it changes the position of its rows). Using ‘rot90’, this transformation can be achieved as follows:

A_rotation = rot90(A,1) B = flipud(A_rotation)

The resulting matrix B will now have swapped row positions:

B = [[3 4] [1 2] [5 6]]

This method of rotation-and-flipping can be useful for specific tasks when traditional row interchanging techniques are not suitable or practical.

  1. Using a loop to perform row swapping A third approach to swapping rows is to write a custom function that takes two indices (i1 and i2) representing the rows to swap and then uses basic array manipulation to change their positions within the matrix. For example, let’s say we have a matrix A with three rows and four columns:

A = [[1 4 7 10] [5 8 11 12] [9 13 14 15]]

To swap the positions of row 1 and row 3, we can write a function similar to this:

function [A_new] = SwapRows(A, i1, i2) A_new = A; % Create a copy of input matrix A. tmpRow = A_new(i1, :); % Store the elements of row i1. A_new(i1, :) = A_new(i2, :); % Assign the elements of row i2 to row i1. A_new(i2, :) = tmpRow; % Place back the saved elements from row i1 to its original position (row i2). end

To apply this function to our example, we can write:

A = [[1 4 7 10] [5 8 11 12] [9 13 14 15]] A_new = SwapRows(A, 1, 3)

After running the function, our matrix will now look like this:

A_new = [[9 13 14 15] [5 8 11 12] [1 4 7 10]]

Here, we have successfully swapped rows 1 and 3 of the initial matrix using a custom function. While this method may not be as efficient for large matrices, it demonstrates an alternative approach to row interchanging.


In conclusion, swapping rows in MATLAB is an essential skill that can significantly enhance your problem-solving capabilities across various domains such as machine learning and data science. By mastering the techniques outlined above - using built-in functions like ‘flipud’ and ‘rot90’, manipulating arrays with loops or writing custom functions, you can confidently apply this technique when necessary to obtain the desired results from your MATLAB scripts.