How Does MATLAB Store Matrices in Memory?

MATLAB stores matrices in memory as a single column of elements.


Create a matrix using the following code:

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

Matrix storage

MATLAB stores every matrix, independently of its shape, as a column vector. This vector results from the concatenation of each column in the matrix, from left to right. If a matrix has more than two dimensions, each plane is processed consecutively.

So, matrix A is stored in memory as the sequence:

1, 3, 5, 6, 2, 4

The order of appearance of each element in the sequence is known as its linear index, counting from 1 (Fig. 1). The concept of linear index is critical to understand linear indexing.

Representation of linear indexing in 2D and 3D matrixes.
Figure 1. Linear index of elements in 2D and 3D matrices.

Further reading

I recommend the following books to learn more on matrix indexing and manipulation in MATLAB:

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a Textbook Excellence Award. Linear indexing, and other indexing methods, are explained clearly in the second chapter.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The fourth chapter is completely dedicated to matrix manipulation.

I also recommend the other tutorials in this series.

Source code

I hope you have understood how MATLAB stores matrices in memory. The source code developed in this tutorial is available at this page.


Your friends may need this knowledge to complete a task. Share this tutorial.