Showing posts with label Array Multiplication. Show all posts
Showing posts with label Array Multiplication. Show all posts

Tuesday, October 7, 2025

Multiplication Operations on Arrays in MATLAB

 

MATLABit

MATLAB, short for MATrix LABoratory, is a powerful programming language and integrated software environment developed by MathWorks. It is widely used in engineering, scientific research, academic instruction, and algorithm development due to its strengths in numerical computation, data analysis, graphical visualization, and simulation. Built on matrix algebra, MATLAB efficiently handles large datasets and complex calculations. In this guide, we will focus on multiplication operations on arrays. Beginners will learn how to multiply array elements, apply element-wise and matrix multiplication, and use MATLAB functions to perform calculations accurately and efficiently.

Table of Contents

Introduction

Multiplication of arrays in MATLAB is one of the most important operations for scientific and engineering applications. Since MATLAB was originally designed as a Matrix Laboratory in the late 1970s by Cleve Moler, special attention has been given to efficient and intuitive handling of matrix and array computations. Unlike some other programming languages where multiplication is limited to scalars, MATLAB provides two distinct but related forms of multiplication: matrix multiplication and element-wise multiplication.

Matrix Multiplication (*)

The operator * in MATLAB follows the rules of linear algebra. This means that if A is an m × n matrix and B is an n × r matrix, then their product A * B results in an m × r matrix. Each element of the result is obtained by computing the dot product of a row of A with a column of B.

For example:

A = [2  4;
     1  3;
     0  5];

B = [3  1;
     2  6];

C = A * B

% The result C is a 3 × 2 matrix:
C = [ (2*3 + 4*2)   (2*1 + 4*6);
      (1*3 + 3*2)   (1*1 + 3*6);
      (0*3 + 5*2)   (0*1 + 5*6) ]

C = [ 14  26;
       9  19;
      10  30 ]

Element-Wise Multiplication (.*)

When two arrays of the same size are multiplied using the operator .*, MATLAB performs element-wise multiplication. In this operation, each component of one array is multiplied by the equivalent component of the other array. This is extremely useful in numerical computing, data analysis, and image processing.

Example:

X = [4  7  2];
Y = [3  5  2];

Z = X .* Y

Z = [ (4*3)  (7*5)  (2*2) ]

Z = [ 12  35  4 ]

Scalar Multiplication

MATLAB also allows direct multiplication of an array by a scalar. In this case, each element of the array is scaled by the scalar value.

M = [1  -2  4;
     0   5  3];

2 * M

ans = [ 2  -4   8;
        0  10   6 ]

Historical Note

Multiplication of matrices is at the heart of linear algebra, which itself serves as the foundation for numerical computing. MATLAB’s design philosophy ensures that both classical matrix multiplication and element-wise operations are easy to perform, without requiring loops. This clear distinction between * and .* reflects MATLAB’s emphasis on combining mathematical precision with programming convenience.

In summary, multiplication of arrays in MATLAB can be carried out in three ways: matrix multiplication (*), element-wise multiplication (.*), and scalar multiplication. Each serves a different purpose, but together they make MATLAB a powerful environment for computational mathematics.

Significance

Multiplication of arrays in MATLAB is a fundamental operation that is used extensively in scientific computing, engineering simulations, and data analysis. MATLAB provides multiple ways to multiply arrays, including element-wise multiplication using the dot operator (.*) and matrix multiplication using the standard asterisk (*) operator. Understanding the difference between these operations is crucial because they serve different purposes and follow different mathematical rules. Proper use of array multiplication allows users to implement algorithms efficiently and accurately, making it one of the most important skills in MATLAB programming.

Element-wise multiplication allows corresponding elements of two arrays of the same size to be multiplied together. This operation is denoted by the dot-asterisk operator (.*) and is commonly used when performing calculations on datasets where each element represents an independent value, such as sensor readings, image pixels, or experimental measurements. For example, multiplying two matrices of the same dimension element by element can model combined effects, scale signals, or apply filters. This method ensures that the operation respects the original layout of the data and avoids unintended results that may occur with standard matrix multiplication.

Matrix multiplication, denoted by the asterisk (*) operator, follows the rules of linear algebra. It requires that the number of columns in the first matrix equals the number of rows in the second matrix. This type of multiplication is widely used in solving systems of equations, performing transformations, and modeling real-world processes such as rotations, scaling, or projections. In MATLAB, matrix multiplication is optimized for efficiency, allowing large-scale computations to be performed quickly, which is essential for simulations, numerical modeling, and engineering calculations.

One of the main significances of multiplication in MATLAB is its role in mathematical modeling. Many scientific problems involve combining quantities through multiplication, such as computing energy, force, or probability values. In linear algebra applications, multiplication of matrices and vectors represents transformations, projections, or rotations in multidimensional space. Proper use of array multiplication ensures that the mathematical meaning of these operations is preserved and that results are accurate.

Multiplication is also essential for element-wise scaling and normalization. For example, multiplying a vector or matrix by a scalar value scales all elements proportionally, which is frequently required in data preprocessing, normalization, or unit conversion tasks. This makes multiplication a versatile tool for both numerical computation and practical applications such as image enhancement or signal amplification.

Another significant advantage of array multiplication in MATLAB is its role in vectorized computations. MATLAB is optimized to perform operations on entire arrays simultaneously, avoiding slow loops and improving code readability. By performing multiplication on arrays directly, users can implement complex calculations with fewer lines of code, reducing the risk of errors and increasing computational efficiency. This vectorized approach is particularly valuable when dealing with large datasets or real-time signal processing tasks.

Matrix multiplication also enables more advanced applications such as solving linear systems, performing eigenvalue decomposition, and implementing algorithms in machine learning and artificial intelligence. For instance, in neural networks, matrix multiplication is used to compute outputs at each layer by multiplying input vectors with weight matrices. This illustrates how multiplication is not only a basic operation but also a foundational tool for modern computational techniques.

All in all, multiplication of arrays in MATLAB is a critical operation for element-wise and matrix-based computations. It allows for scaling, transformation, modeling, and vectorized computation, ensuring accurate, efficient, and meaningful results. Mastery of array multiplication enables users to handle both simple and complex computational tasks, making it an indispensable part of MATLAB programming in scientific, engineering, and data-driven applications.

Multiplication of Arrays in MATLAB

In MATLAB, the multiplication operator * is executed according to the norms of linear algebra. This means that if P and B are two matrices, the operation P * B can only be performed when the number of columns in P are same as the number of rows in B. The result will then be a new matrix with the equivalent rows as P and the equivalent columns as B.

Matrix Multiplication Dimensions

For example, if P is a 4 × 3 matrix and B is a 3 × 2 matrix, then the product P * B will be a 4 × 2 matrix. Each element of the result is obtained as the dot product of a row of P with a column of B:

(P11*B11 + P12*B21 + P13*B31)   (P11*B12 + P12*B22 + P13*B32)
(P21*B11 + P22*B21 + P23*B31)   (P21*B12 + P22*B22 + P23*B32)
(P31*B11 + P32*B21 + P33*B31)   (P31*B12 + P32*B22 + P33*B32)
(P41*B11 + P42*B21 + P43*B31)   (P41*B12 + P42*B22 + P43*B32)

Numerical Example

P = [ -1  0  2;
      7  4  3;
      6  0  8;
      9  2  5 ];   % Define a 4×3 matrix P

B = [ 3  6;
      2  0;
      4  7 ];      % Define a 3×2 matrix B

C = P * B

The result is:

C =
  (-1*3 + 0*2 + 2*4)  (-1*6 + 0*0 + 2*7)
  (7*3 + 4*2 + 3*4)   (7*6 + 4*0 + 3*7)
  (6*3 + 0*2 + 8*4)   (6*6 + 0*0 + 8*7)
  (9*3 + 2*2 + 5*4)   (9*6 + 2*0 + 5*7)

C =
  5   8
  41   63
  50   92
  49   89

Non-Commutativity

It is essential to keep that in mind that matrix multiplication is not commutative. In other words, A * B does not necessarily equal B * A. In fact, in the example above, trying B * A produces an error since the dimensions are not compatible (B has 2 columns, while A has 4 rows).

Multiplying Square Matrices

F = [ 2  4;
      1  3 ];

G = [ 5  7;
      0  6 ];

F * G

The result is:

ans =
  (2*5 + 4*0)   (2*7 + 4*6)
  (1*5 + 3*0)   (1*7 + 3*6)

ans =
  10   38
   5   25
G * F

The result is different:

ans =
  (5*2 + 7*1)   (5*4 + 7*3)
  (0*2 + 6*1)   (0*4 + 6*3)

ans =
  17   41
   6   18

This confirms that F * G ≠ G * F.

Vector Multiplication

Two vectors can be multiplied if they have the same number of elements, however, one must be expressed as a horizontal vector, while the other should be represented as a vertical vector:

AV = [ 4  2  5 ];   % Horizontal vector
BV = [ -2;
       -3;
        0];         % Vertical vector

AV * BV    % Row × Column → Scalar (dot product)
ans = -14

BV * AV    % Column × Row → 3×3 matrix
ans =
  -8    -4   -10
  -12   -6   -15
   0    0    0

Scalar Multiplication

When an array is multiplied by a scalar (a single number, treated as a 1 × 1 array), every element in the array is multiplied by that scalar:

A = [ 3  0  2  -1;
      1  4  8  6;
      9  0  3  2 ];   % Define a 3×4 matrix

b = 4;

b * A

The result is:

ans =
  12  0   8  -4
   4  16  32  24
  36   0  12   8

Connection to Systems of Linear Equations

Linear algebra rules of array multiplication provide a convenient way of expressing systems of equations. For instance, the system:

2x1 + 3x2 + 4x3 = 15
1x1 + 5x2 + 2x3 = 20
3x1 + 0x2 + 6x3 = 25

can be written as:

[ 2  3  4 ]   [ x1 ]   [ 15 ]
[ 1  5  2 ] * [ x2 ] = [ 20 ]
[ 3  0  6 ]   [ x3 ]   [ 25 ]

or more compactly in matrix notation as: A * X = B.

Applications

Array multiplication is one of the most powerful tools in MATLAB, especially because it follows the rules of linear algebra. It is widely applied in scientific computing, engineering, data analysis, and machine learning. Since MATLAB is designed for matrix-based computations, multiplication is at the heart of most real-world applications.

1. Solving Systems of Linear Equations

Many real-world problems can be expressed as a system of linear equations. Using matrix multiplication, these systems can be written compactly as A × X = B, where A is a coefficient matrix, X is the vector of unknowns, and B is the constants vector. MATLAB efficiently solves such systems using matrix operations instead of handling each equation individually.


A = [3 2 1; 4 5 6; 7 8 9];   % Coefficient matrix
X = [x1; x2; x3];            % Unknowns
B = [12; 30; 45];            % Constants
% System representation: A * X = B
  

2. Computer Graphics and Transformations

In graphics and visualization, transformations such as rotation, scaling, and translation are performed using matrix multiplication. For example, a 2D point or an image can be rotated around the origin using a rotation matrix multiplied by the vector of coordinates.


theta = pi/4;   % Rotation angle (45 degrees)
R = [cos(theta) -sin(theta); 
     sin(theta)  cos(theta)];
P = [5; 2];     % A point in 2D space
NewP = R * P;   % Rotated coordinates
  

3. Signal Processing

In digital signal processing (DSP), array multiplication is used for filtering, convolution, and Fourier transforms. By multiplying signals with transformation matrices, MATLAB helps in analyzing signals in time and frequency domains.

4. Machine Learning and Artificial Intelligence

Neural networks, regression models, and optimization algorithms rely heavily on array multiplication. Weight matrices in machine learning models are multiplied with input data arrays to generate predictions. MATLAB's matrix operations make training and testing computational models efficient.

5. Engineering Applications

In electrical, mechanical, and civil engineering, MATLAB uses array multiplication for structural analysis, circuit design, and control systems. For instance, state-space representations in control systems are solved directly using matrix multiplication.

6. Economics and Data Analysis

In finance and economics, matrix multiplication is used for portfolio optimization, input-output models, and economic forecasting. Large datasets can be processed quickly through vectorized operations, making MATLAB ideal for quantitative research.

Summary

From solving equations and designing engineering systems to training AI models and simulating graphics, array multiplication in MATLAB provides a universal and efficient framework for computation. It not only simplifies mathematical operations but also ensures high performance when working with large-scale problems.

Conclusion

Array multiplication in MATLAB is not just a mathematical operation, but a foundation for solving complex computational problems. Unlike element-wise operations, matrix multiplication strictly follows the norms of linear algebra and ensuring mathematical consistency.

Through examples, we have seen that multiplication can be performed between matrices, vectors, and scalars, each following specific dimension rules. While matrix-by-matrix multiplication allows us to handle systems of equations and transformations, scalar multiplication scales every element of an array, and vector multiplication provides dot and cross products that are widely used in geometry and physics.

One of the most important takeaways is that matrix multiplication is not commutative, meaning A × B ≠ B × A in most cases. This property highlights the need for careful attention to dimensions and order of operations when performing calculations.

Overall, array multiplication provides a powerful framework for applications in engineering, computer science, data analysis, economics, graphics, and machine learning. Mastering this concept in MATLAB allows users to efficiently model, analyze, and solve real-world problems with accuracy and speed.

In short: Understanding and applying matrix multiplication in MATLAB equips learners and professionals with one of the most essential tools in numerical computing.

© 2025 MATLABit. All rights reserved.

Logarithmic Plotting in MATLAB: How to Use Log Axes for Scientific Data Visualization

  MATLABit MATLAB (MATrix LABoratory) is a high-level programming language and numerical computing environment developed by MathWorks, w...