Matlab

How to Fix “Dimensions of arrays being concatenated are not consistent” Error in MATLAB

MATLAB is a helpful programming tool that allows us to concatenate two or more vectors, matrices, and arrays. Sometimes while concatenating vectors, matrices, and arrays we get an error “Dimensions of arrays being concatenated are not consistent” in MATLAB.

This guide will explore how to fix the error “Dimensions of arrays being concatenated are not consistent” in MATLAB.

How to Fix the Error “Dimensions of arrays being concatenated are not consistent” in MATLAB?

There are some reasons why we can get an error “Dimensions of arrays being concatenated are not consistent”. This error occurs when:

  • Vectors, matrices, and arrays being concatenated horizontally do not have an equal number of rows.
  • Vectors, matrices, and arrays concatenated vertically don’t have an equal count of columns.
  • While writing a matrix, the number of elements in the rows are not equal.

This error can be fixed through the following solutions.

  • When arrays are being concatenated horizontally, they must have an equal number of rows.
  • When arrays are concatenated vertically, they must have an equal number of columns.
  • When we write a matrix, the count of entries in each row must be equal.

Solution Examples

Follow these solutions to learn how to fix the error “Dimensions of arrays being concatenated are not consistent” in MATLAB.

Solution 1: How to Fix the Error “Dimensions of arrays being concatenated are not consistent” while Concatenating the arrays Horizontally?

In this example, we define a row vector x and a column vector y. Then we concatenate these vectors horizontally.

x = [1 3 5 7 9];

y = [2;4;6];

z = [x y]

When we execute this code, we will get an error “Dimensions of arrays being concatenated are not consistent” as shown on the screen.

This error occurred in line 3 because the count of rows in x is not equal to the count in y. This error can be fixed by defining an equal count of rows in both x and y as shown in the given MATLAB code.

x = [1 3 5 7 9];

y = [2 4 6];

z = [x y]

Solution 2: How to Fix the Error “Dimensions of arrays being concatenated are not consistent” while Concatenating the Arrays Vertically?

The given example defines a matrix x having a size 2-by-3 and a matrix y having a size 3-by-4. Then we concatenate these vector matrices vertically.

x = rand(2,3);

y = ones(3,4);

z = [x;y]

When we execute this code, we will get an error “Dimensions of arrays being concatenated are not consistent” as shown on the screen.

This error occurred in line 3 because the count of columns in x is not equal to the count of columns in y. This error can be fixed by defining an equal count of columns in both x and y as shown in the given MATLAB code.

x = rand(2,3);

y = ones(3,3);

z = [x;y]

Solution 3: How to Fix the Error “Dimensions of arrays being concatenated are not consistent” While Writing a Matrix?

This MATLAB code defines a matrix x.

x = [1 2 3;2 4];

When we execute this code, we will get an error “Dimensions of arrays being concatenated are not consistent” as shown on the screen.

This error occurred in line 1 because the number of elements in each row of x are not equal. This error can be fixed by defining an equal number of elements in each row of x as shown in the given MATLAB code.

x = [1 2 3;2 4 6]

Conclusion

In MATLAB programming, we can get different errors due to different reasons. One such error is “Dimensions of arrays being concatenated are not consistent”. This error can occur while concatenating arrays horizontally or vertically and while writing a matrix. In this guide, we have explored how to fix the error “Dimensions of arrays being concatenated are not consistent” in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.