
- 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 - Array Multiplication
Array multiplication in MATLAB involves performing operations on arrays of numbers.
Element-wise multiplication is used when you want to multiply corresponding elements of two arrays together. This type of multiplication is denoted using the .* operator.
The arrays being multiplied must have the same dimensions. Each element in the resulting array is obtained by multiplying the corresponding elements from the original arrays.
Syntax
X = A.*B X = times(A,B)
X = A.*B performs array multiplication between A and B by calculating the product of their corresponding elements. It's important to ensure that A and B have either identical sizes or sizes that are compatible for this operation.
The usage of C = times(A,B) as an alternative method to compute A.*B exists, although it's infrequently employed. This approach allows for operator overloading in classes.
Example 1
Multiplying two vectors A and B.
A = [1 2 3] B = [4 5 6] X = A.*B
When you execute the same in matlab command window −
A = 1 2 3 B = 4 5 6 X = 4 10 18
Example 2
Let us try another example by using a matrix of 2x3 as shown below −
A = [1, 2, 3; 4, 5, 6] B = [2, 2, 2; 3, 3, 3] C = A .* B
When you execute the same in matlab command window the output is −
A = 1 2 3 4 5 6 B = 2 2 2 3 3 3 C = 2 4 6 12 15 18
The matrix C is also the same size as of A and B.
Let us now try to make use of the times() method for the above examples we have tried using .*.
Example 3
Multiplying two vectors A and B using the times() method.
A = [1 2 3] B = [4 5 6] X = times(A,B)
When you execute the same in matlab command window −
A = 1 2 3 B = 4 5 6 X = 4 10 18
Example 4
Let us try another example by using a matrix of 2x3 but will multiply them using the times() method.
A = [1, 2, 3; 4, 5, 6] B = [2, 2, 2; 3, 3, 3] C = times(A,B)
When you execute the same in matlab command window −
A = 1 2 3 4 5 6 B = 2 2 2 3 3 3 C = 2 4 6 12 15 18
Multiply Row and Column Vectors
The row vector with dimensions 1-by-3 and the column vector with dimensions 4-by-1 are multiplied in the example below, resulting in the creation of a 4-by-3 matrix.
a = 1:3 b = (1:4)' result_matrix = a .* b
In this example, 'a' is the row vector [1, 2, 3], and 'b' is the column vector . The ' operator is the transpose operator. When applied to a matrix or an array, it flips the rows and columns, effectively transforming rows into columns and columns into rows.
The expression (1:4) generates the row vector [1, 2, 3, 4].Applying the transpose operator ' to the row vector converts it into a column vector −
b = 1 2 3 4
When these vectors are multiplied element-wise, the resulting matrix 'result_matrix' will be −
a = 1 2 3 b = 1 2 3 4 result_matrix = 1 2 3 2 4 6 3 6 9 4 8 12