Thursday, August 14, 2025

Creating Matrices in MATLAB

 

MATLABit

MATLAB stands for MATrix LABoratory. It’s a powerful programming language and software tool created by MathWorks. Its extensive application across engineering, scientific research, academic instruction, and algorithmic design stems from its strengths in numerical computation, data analysis, graphical visualization, and simulation. With a foundation in matrix algebra, MATLAB efficiently manages large datasets and complex mathematical models. So, let's begin to create matrices in MATLAB.

Table of Contents

Introduction

Numbers are arranged in rows and columns forming a two-dimensional array, also known as a matrix. Matrices can be used to store information like a table. They describe a wide range of physical quantities in science and engineering and are crucial to linear algebra.

Square and Rectangular Matrices

The number of rows and columns in a square matrix are equal. For instance, the below given matrix is 4 × 4:

t1t2t3t4
t5t6t7t8
t9t10t11t12
t13t14t15t16

Size: 4 × 4

Also, for 5 × 4:

2114307
3251911
2861522
179345
13182420

Size: 5 × 4

There are m rows and n columns in a m × n matrix. The matrix's size is "m by n."

Creating a Matrix (Row by Row)

By allocating the matrix's components to a variable, a matrix is produced. Insert each element a single at a time inside square brackets [ ]. Use commas or spaces to divide consecutive elements. To start a new row, use a semicolon (;) or press Enter. Close with the right bracket ].

variable_name = [elements in the first, second, and third rows; 
... ; last row elements]

Example (4 × 4):

A = [t1 t2 t3 t4; t5 t6 t7 t8; t9 t10 t11 t12; t13 t14 t15 t16]

Example (5 × 4):

B = [21 14 30 7; 3 25 19 11; 28 6 15 22; 17 9 34 5; 13 18 24 20]

Other Ways to Create Matrices

Numbers or mathematical expressions comprising numbers, functions, and predefined variables can be entered as elements. Each row has to comprise the identical number of elements. Enter 0 if an element is zero. If you try to define an incomplete matrix, MATLAB will show you an error message.

MATLAB example:

> A = [5:2:15; 5:5:30; linspace(40,90,6); 5 4 3 7 8 0]

A =

     5     7     9     11     13    15
      5     10    15    20    25    30
      40    50    60    70    80    90
      5     4     3     7     8      0 

>>

Examples of expressions in matrix elements

Matrix elements may be simple numbers or mathematical expressions. The expressions are evaluated when the matrix is created, so you can use arithmetic, variables, and MATLAB functions.

Arithmetic expressions:

> A = [3+1 2-9 4*5; 7^2 7+0 3-2]

A =  4  -7  20
     49  7  1

     

Using variables:

> y = 5;
>> B = [y*3 2+y y^0; y-1 y y^2]

B =
     15  7  1
     4  5  25
    
    

Using functions and constants:

> C = [sqrt(16) sin(pi/2) cos(0); linspace(50,52,3)]

C =

     4     1     1
    50     51    52

Zeros and explicit entries:

> D = [2 1 2; 5 0 7]

D =

     2     1     2
     5     0     7

What causes an error:

> % Rows with different numbers of elements cause an error
>> E = [1 2 3; 4 5]

Error using <...>
Matrix dimensions must agree.

These examples show how flexible MATLAB is when building matrices: you can mix arithmetic, variables, and functions as long as each row has the same number of evaluated elements.

Applications of Creating Matrices in MATLAB

Matrices are used throughout scientific work and technology. Below are common application areas with short MATLAB-style examples to show how matrices appear in practice.

1. Solving linear systems

It is possible to reduce plenty of physical models to Lx = c systems of linear equations. MATLAB solves these efficiently with the backslash operator.

> L = [3 2; 1 4];
>> c = [5; 6];
>> x = L \ c;   % solve Lx = c

2. 2D transformations and computer graphics

Rotations, scaling, and shearing are represented by matrices. A matrix multiply is the process of applying a transformation to a vector.

> theta = pi/6;                     % 30 degrees
>> T = [cos(theta) -sin(theta); sin(theta) cos(theta)];
>> vec = [1; 0];
>> R0 = T * vec;                     % rotated vector

3. Image processing

Digital images are matrices (grayscale) or 3D arrays (RGB). Matrix operations perform filtering, resizing, and color transforms.

> I = imread('coins.png');          % image stored as matrix/array
>> s = size(I);
>> J = imresize(I, 0.5);              % resize using matrix interpolation

4. Engineering and physics

Matrices appear in finite-element models, state-space models for control systems, stress/strain tensors, and more.

These examples illustrate how matrices provide a compact, uniform way to represent and manipulate structured numerical data. Because MATLAB and similar environments are optimized for matrix operations, many algorithms are implemented by combining a few concise matrix commands.

Conclusion

Matrices are a fundamental way to organise numerical information into rows and columns. They can be square or rectangular (an m × n matrix has m rows and n columns), and their elements can be numbers or evaluated mathematical expressions, including variables and functions. MATLAB makes it easy to create and manipulate matrices using concise row-by-row notation, ranges, and built-in functions. Because many scientific, engineering, and data problems reduce to structured numerical operations, matrices—and efficient matrix operations—are central tools in computation and analysis.

© 2025 MATLABit. All rights reserved.

No comments:

Post a Comment

Division Operation Applied to Arrays in MATLAB

  MATLABit MATLAB stands for MATrix LABoratory. It’s a powerful programming language and software tool created by MathWorks. Its extensiv...