Lab# 03
Mathematical Operations with
Arrays in MATLAB
Introduction
Arrays are fundamental to MATLAB — in fact, the name MATLAB stands for MATrix LABoratory, and
arrays (especially matrices) are at the heart of how MATLAB works.
Types of Arrays
➤ Vectors (1D arrays)
Row vector:
Row Vector = [1, 2, 3, 4];
Column vector:
Column Vector = [1; 2; 3; 4];
➤ Matrices (2D arrays)
Matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];
➤ Multidimensional Arrays
Array3D = randi(3, 3, 2); % A 3x3x2 3D array
Array Creation Functions
Function Description
zeros(m,n) Creates an m×n array of zeros
ones(m,n) Creates an m×n array of ones
rand(m,n) Random numbers between 0 and 1
eye(n) Identity matrix
linspace(a,b,n) Linearly spaced vector
colon operator(:) Range creation, e.g., 1:5
Accessing and Modifying Arrays
A = [10, 20, 30; 40, 50, 60];
val = A(2,3); % Access element at row 2, column 3
A(1,2) = 99; % Modify value at (1,2)
row = A(2,:); % All columns in row 2
col = A(:,1); % All rows in column 1
Array Operations
Element-wise operations (with .):
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A .* B; % Element-wise multiplication
D = A .^ 2; % Element-wise power
Matrix multiplication:
C = A * B; % Only valid if dimensions match for matrix multiplication
Useful Functions for Arrays
Function Description
size(A) Size of array A
length(A) Length of the largest dimension
reshape(A,m,n) Reshapes A into m-by-n
transpose(A) or A.' Transpose of A
sum(A) Sum of elements (column-wise by default)
mean(A) Mean of elements
sort(A) Sorts the array
Creating a One Dimensional Array vector
A one dimensional array is a list of numbers arranged in a row or column.
Creating a Vector from Known List of Numbers
The Vector is created by typing the known numbers in the square brackets in the command window. The
two types of vectors are as:
Row Vectors
Column Vectors
Creating a Vector with Constant Spacing by
Specifying First Term
In a vector with constant spacing the difference between the elements is same.
Creating a Vector with Linear Spacing by
Specifying the First and Last Terms and Number of
Terms
A vector with the n elements that are linearly spaced in which the first element is xi and the last element
is xf can be created by typing the ‘linspace command’
Creating Two Dimensional Array Matrix
A two dimensional array also called matrix has numbers in rows and columns. Matrices can be used to
store information like the arrangements in a table. Matrices play an important role in linear algebra and
are used in science and engineering to describe many physical quantities.
The Zeros, Ones and Eye Commands
The zeros(m,n), ones(m,n) and eye(n) commands can be used to create matrices that have elements
with special values. The zeros and ones commands create a matrices of 0 or 1 numbers respectively.
The eye command creates a square matrix with n rows and n columns in which the diagonal elements
are equal to 1 and rest of the elements are 0.
Build-in Array Functions
Array Creation Functions
Function Description
zeros(m,n) Creates an m×n array of zeros
ones(m,n) Creates an m×n array of ones
eye(n) Creates an n×n identity matrix
rand(m,n) Creates an m×n array of uniformly distributed random numbers (0 to 1)
randn(m,n) Normally distributed random numbers (mean 0, std 1)
linspace(a,b,n) Generates a row vector of n equally spaced points between a and b
logspace(a,b,n) Logarithmically spaced vector from 10^a to 10^b
colon (a:b) Creates a row vector from a to b with step 1
colon (a:step:b) Creates a row vector from a to b with custom step
Array Information Functions
Function Description
size(A) Returns the size (dimensions) of array A
Returns the length of the largest
length(A)
dimension
ndims(A) Number of dimensions in array A
numel(A) Total number of elements in array A
Function Description
isempty(A) Returns true if A is an empty array
isvector(A) Returns true if A is a vector
ismatrix(A) Returns true if A is a matrix
isnumeric(A) Checks if A is a numeric array
islogical(A) Checks if A is logical (true/false)
Array Manipulation / Reshaping
Function Description
reshape(A,m,n) Reshapes array A into size m×n
transpose(A) or A.' Transpose of A (no complex conjugate)
A' Complex conjugate transpose
squeeze(A) Removes singleton dimensions (size 1)
permute(A,order) Rearranges the order of dimensions
flip(A) Flips elements in all dimensions
fliplr(A) Flips array left to right
flipud(A) Flips array up to down
rot90(A) Rotates matrix 90 degrees counterclockwise
cat(dim,A,B) Concatenates arrays along specified dimension
horzcat(A,B) Horizontal concatenation
vertcat(A,B) Vertical concatenation
Array Indexing & Searching
Function Description
find(A) Returns indices of non-zero elements
sub2ind(size, i, j) Converts row-column subscripts to linear index
ind2sub(size, idx) Converts linear index to row-column subscripts
any(A) Returns true if any element is nonzero
Function Description
all(A) Returns true if all elements are nonzero
Mathematical Array Operations
Function Description
sum(A) Sum of elements along a dimension
mean(A) Mean of elements
median(A) Median of elements
std(A) Standard deviation
var(A) Variance
min(A) Minimum value in array
max(A) Maximum value in array
prod(A) Product of elements
cumsum(A) Cumulative sum
cumprod(A) Cumulative product
mod(A,B) Modulus after division (A mod B)
Sorting and Set Operations
Function Description
Sorts elements in ascending
sort(A)
order
sortrows(A) Sorts rows of a matrix
unique(A) Returns unique elements
union(A,B) Union of sets A and B
intersect(A,B) Intersection of sets
setdiff(A,B) Elements in A but not in B
Logical Operations on Arrays
Function Description
==, ~=, >, < Element-wise logical comparisons
`& , , ~`
xor(A,B) Logical exclusive OR
isnan(A) Returns true where A is NaN
isfinite(A) Returns true for finite elements