MATLAB - Compatible Arrays



In MATLAB, compatible arrays are considered compatible when they share the same data type and size, or if one of them is a scalar. During the execution of element-wise operations or functions in MATLAB, arrays with compatible sizes are automatically extended to align with the dimensions of each other.

MATLAB-Compatible Arrays can hold various types of data, that includes numbers, strings, logical values, and more. They are particularly optimized for handling numerical data, making MATLAB a powerful tool for scientific and engineering applications.

Here are a few examples of scalars , vectors and matrices that have compatible sizes.

Row and Column Vector Compatibility

Row and column vectors consistently possess compatible sizes, even when their sizes and lengths differ. Engaging in arithmetic operations involving these vectors results in the creation of a matrix.

Example 1

Consider you have a 2x2 matrix. In addition the output is also a 2x2 matrix.

matrix1 = [1, 2; 3, 4]
matrix2 = [5, 6; 7, 8]

In this example, matrix1 and matrix2 are two 2x2 matrices. When you see the output inside the matlab command window the matrix1 and matrix2 are displayed as shown below.

>> matrix1 = [1, 2; 3, 4]
matrix2 = [5, 6; 7, 8]

matrix1 =

   1     2
   3     4

matrix2 =

   5     6
   7     8

>>

Now let us perform addition on it as shown below −

result = matrix1 + matrix2

Now when you execute above line in matlab command window the output is as follows −

>> result = matrix1 + matrix2

result =

   6     8
   10    12

>>

You can see, the result of adding two 2x2 matrices element-wise is also a 2x2 matrix with corresponding elements added together.

Example 2

Adding a scalar to a 2x2 matrix.

matrix = [1, 2; 3, 4]
scalar = 5

In this example, matrix is a 2x2 matrix and scalar is a scalar value. When you check the same in matlab command window you will get −

>> matrix = [1, 2; 3, 4]
scalar = 5

matrix =

   1     2
   3     4

scalar = 5

Now let us add the scalar with the matrix as shown below −

result = matrix + scalar

Now let us check the above in the command window of matlab −

>> result = matrix + scalar

result =

   6     7
   8     9

>> 

You can see, each element of the original matrix has been increased by the scalar value, resulting in a new 2x2 matrix in the end.

Example 3

Here we are going to make use of the 4x2 matrix and add it to a column vector 4x1.

matrix = [1, 2; 3, 4; 5, 6; 7, 8]
column_vector = [10; 20; 30; 40]

When you check the above in matlab command window we get −

>> matrix = [1, 2; 3, 4; 5, 6; 7, 8]
column_vector = [10; 20; 30; 40]

matrix =

   1     2
   3     4
   5     6
   7     8

column_vector =

   10
   20
   30
   40

>>

Here the matrix is 4x2 matrix i.e it has 4 rows and 2 columns. We have column_vector 4x1 i.e 4 rows and 1 column.

Let us add the two and we are going to see the rows remain intact i.e the output will have a size 4x2.

result = matrix + column_vector

Now when you check the output in matlab command window the result is −

>> result = matrix + column_vector

result =

   11    12
   23    24
   35    36
   47    48

>>

Each element in the column vector has been added to the corresponding column of the original matrix, resulting in a new 4x2 matrix.

Example 4

Here we are going to see the result of adding a column vector with a row vector.

column_vector = [1; 2; 3; 4]
row_vector = [5, 6, 7, 8]

Let us check the output in matlab command window −

>> column_vector = [1; 2; 3; 4]
row_vector = [5, 6, 7, 8]

column_vector =

   1
   2
   3
   4

row_vector =

   5     6     7     8

>>

The column_vector has size 4x1 and the row_vector has size 1 x 4.

Now let us see the result of adding both of them.

result = column_vector + row_vector

The output in matlab command window is −

>> result = column_vector + row_vector

result =

   6     7     8     9
   7     8     9    10
   8     9    10    11
   9    10    11    12

>>

The size of the result matrix is 4x4. Each element in the column vector is added to the corresponding element in the row vector, resulting in a matrix where each element is the sum of the corresponding elements from the input vectors.

Example 5

Let us also perform one multiplication and subtraction operation on the Row and column vector.

column_vector = [1; 2; 3; 4]
row_vector = [5, 6, 7, 8]
result = column_vector *  row_vector

The output when you execute the same in matlab command window −

>> column_vector = [1; 2; 3; 4]
row_vector = [5, 6, 7, 8]
result = column_vector *  row_vector

column_vector =

   1
   2
   3
   4

row_vector =

   5     6     7     8

result =

   5     6     7     8
   10    12    14    16
   15    18    21    24
   20    24    28    32

>> 

Let us now perform subtraction operation.

column_vector = [1; 2; 3; 4]
row_vector = [5, 6, 7, 8]
result = column_vector -  row_vector

The output when you execute the same in matlab command window −

>> column_vector = [1; 2; 3; 4]
row_vector = [5, 6, 7, 8]
result = column_vector -  row_vector

column_vector =

   1
   2
   3
   4

row_vector =

   5     6     7     8

result =

   -4    -5    -6    -7
   -3    -4    -5    -6
   -2    -3    -4    -5
   -1    -2    -3    -4

>>
Advertisements