Subscripted Assignment Dimension Mismatch – Errors in Matlab

The errors are a fundamental part of our lives, interact or not with MATLAB. The only difference is that, when we interact with computers, our errors are immediately indicated to us. It should be noted that programming in MATLAB requires time and concentration. If we do not have the time or the right concentration it can become very irritating, if for a moment we do not make the distinction between (, [or {or simply we do not distinguish between: and, or we forget that ‘a’ is different from ‘A’, then programming in MATLAB will become long and tedious.

One of the most common mistakes that we will encounter while working with MATLAB is ‘Subscripted assignment dimension mismatch’. Following is an examplethat leads to this error message.

>> D=zeros(3); d=[1,2];


Subscripted assignment dimension mismatch.

This error occurs when there are matrix assignments and the matrices that are on both sides of the = sign do not have the same dimension. Use the size () command to check the dimensions of both elements and make sure they match. In this example, we would have to


ans =

2              3


ans =

1              2

The “Mismatched Dimension Mismatch” error

The “Mismatched Dimension Mismatch” error means that you are trying to assign a block of values in a space that is the wrong size.

This entity, –

 mcmvOUT2((K+1)):(k+Nz), i:Nt:Nr;

-, represents a matrix of values ​​in 2 dimensions. Its size is defined by the two ranges specified by and – you can check its size by typing(k+1):(k+Nz)i:Nt:Nr

 size(mcmvOUT2((K+1)):(k+Nz), i:Nt:Nr;))

The space you try to insert it in must be exactly the same dimensions. The size of the range specified by the following,


is defined by the Nzand Nxarguments to the call zeroswith which you pre-allocated the array.

We cannot test this because the file containing your data is not given, but you can solve it yourself using the command size and making sure all your dimensions match. Because Matlab uses column indexing, and many of us are used to the paradigm of Cartesian coordinate systems, the order of your indexes can be confusing – it’s the root of many of these systems kinds of mistakes. Things to check: your dimensions Nz etc. are correct and the order of your variables Nz etc. in the call zeros is correct.

Understand how Matlab stores Matrices: Indexing of matrices in Matlab

In order to prevent this error, you must understand the basic indexing of matrices as performed by Matlab.

Basically, MatLab stores vectors and matrices, regardless of their dimension, as column vectors.  For example, the following matrix:

 (2 7 4) (5 8 3)

it is stored in a column vector formed by the columns of the matrix (one column after another).

 (two) (5) (7) (8) (4) (3)

This way of storing in MatLab implies that it is possible to access the elements of a matrix with a single index (ranging from 1 to the total number of elements in the matrix).

In the case of matrices, we have just seen that the corresponding column vector was simply composed of the columns of the matrix placed one after the other. However, it is more difficult to see what happens when we manipulate arrays of more than 2 dimensions.

Let’s see the particular case of a three-dimensional matrix T, of 4X2X3. This matrix contains 24elements. If we number them from 1 to 24 and consider (for clarity) that a 3- dimensional matrix is a set of pages (last dimension) containing each of the matrices (the first two dimensions), then they will be organized in this way:


Therefore, these elements are organized in the vector column that corresponds to the matrix by increasing the first index of the matrix, then the second, then the third (and the next ones if we worked with more than 3 dimensions).
Next let’s see how we can show the elements of T in the order in which it is stored. First, assign a value to T:

 T = rand (4,2,3);

In the corresponding column vector, the elements are given in the order in which they are stored by:

 for p = 1: 3 for n = 1: 2 for m = 1: 4 disp (T (m, n, p)); end end end

In other words, starting from a column vector corresponding to the storage of a matrix, it is sorted in the matrix by dividing it according to the last dimension, then the preceding one, and so on. Therefore, the division is axed in this way:


Finally, in our example, we can access the eleventh element of matrix T in two ways:

 T (3, 1, 2)


 T (11)

You just have to write the following lines to see that the elements appear in the same order as in the 3 preceding loops.

 for q = 1: 24 disp (T (q)); end


Leave a Reply
