
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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 >>