0% found this document useful (0 votes)
4 views15 pages

Array

The document provides an overview of programming tools for mathematics in MATLAB, focusing on arrays as fundamental data objects. It covers one-dimensional arrays (vectors), two-dimensional arrays (matrices), array addressing, common functions, and operations, including arithmetic and sorting. Additionally, it discusses cell arrays, rectangular character arrays, and string functions, highlighting the differences between character arrays and the modern string data type.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Array

The document provides an overview of programming tools for mathematics in MATLAB, focusing on arrays as fundamental data objects. It covers one-dimensional arrays (vectors), two-dimensional arrays (matrices), array addressing, common functions, and operations, including arithmetic and sorting. Additionally, it discusses cell arrays, rectangular character arrays, and string functions, highlighting the differences between character arrays and the modern string data type.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Tools for Mathematics

Arrays in MATLAB
Day 2
Array
● Arrays are the fundamental data objects in MATLAB®, acting as the core structure
for storing and manipulating data.
● In MATLAB, all variables are multidimensional arrays (numeric, character, or
logical), regardless of their type.
● A 1D array is a vector, and a 2D array is a matrix.
One-Dimensional Arrays (Vectors)
● In MATLAB, a one-dimensional array is called a vector. Vectors are the basic
building blocks for mathematical computation.
A = [2 4 6 8 10]; %Row vector

B = [1; 3; 5; 7; 9]; %Column vector


C = 1:6;
D = 0:0.25:1;
Two-Dimensional Arrays (Matrices)
● A two-dimensional array is known as a matrix.

M = [1 2 3; 4 5 6; 7 8 9];

size(M) %dimension of matrix(m,n)

length(M)%max{m,n}
Array Addressing (Indexing)
● MATLAB follows 1-based indexing, unlike many other programming languages.

A = [10 20 30 40 50];
A(4) %Indexing vector

M = [1 2 3; 4 5 6; 7 8 9];
M(2,3) %Indexing Matrices

M(1,:) % First row Extraction


M(:,2) % Second column Extraction
M(2:3,1:2)%SubmatrixExtraction a21, a22, a31, a32
Common Array Functions
%Vector based Functions
A = [5 12 7 9 3];
max(A)
min(A)
sum(A)
mean(A)
std(A)

%Matrix based Functions


M = [2 1; 4 3];
det(M) %determinant
trace(M) %trace
rank(M) %ramk
eig(M) %eigenvalue
[U,R]=eig(M] %both eigenvalues & eigenvectors
Sorting Arrays
%Sorting Vectors
A = [9 2 7 1 5];
sort(A)
sort(A,'descend')

%Sorting Matrices Column-wise


M = [3 8 1; 6 2 9];
sort(M)
Cell Arrays
Cell arrays can store heterogeneous data such as numbers, vectors, matrices, and strings.

C = {25, 'MATLAB', [1 3 5 7]};


%Displaying Cell Contents
C{2}

C(2)

%Accessing Elements Inside Cells


C{3}(4) %4th element of cell 3
Arithmetic Operations with Arrays
%Element-wise Operations
A = [2 4 6];
B = [1 3 5];

A + B
A .* B
A .^ 2

%Matrix Operations
M1 = [1 2; 3 4];
M2 = [2 0; 1 2];

M1 * M2 %Matrix Multiplication
Rectangular Character Arrays
A rectangular character array (also called a character matrix) is a 2‑D array of characters
where each row must have the same number of characters.
MATLAB stores characters internally as arrays, just like numbers.
S = ['Math'; 'Code'];
S(2,:)

Explanation:

● 'Math' has 4 characters


● 'Code' has 4 characters
● Hence, a valid rectangular character array

S = ['Cat'; 'Elephant'];
❌ This will cause an error because the number of characters is not equal.

S = ['Cat '; 'Elephant']; %extra spaces in Cat


Rectangular Character Arrays

% Accessing Characters and Rows


S(1,:) % First row
S(:,2) % Second column (characters)
S(2,1:3) % First three characters of second row

size(S)
length(S)
Combining Strings into a Cell Array
Rectangular character arrays are restrictive because all strings must have equal length. To
overcome this limitation, MATLAB uses cell arrays of strings.

Each cell can store a string of any length.


%Creating a Cell Array of Strings

names = {'Ram', 'Sita', 'Lakshman', 'Arjun'}

%Accessing Cell Array Elements


names(2)
Names{2}

( ) returns a cell
{ } returns the string inside the cell
Combining Strings into a Cell Array
%Using Strings from Cell Arrays
length(names{3})

%Cell Arrays with Mixed Data


student = {101, 'Amit', 87.5};
student{2}
String Functions in MATLAB
MATLAB supports two string representations:

Character arrays (single quotes ' ')

String data type (double quotes " ")


%Character Array Functions
str = 'Programming in MATLAB';
length(str)
upper(str) %String in upper case
lower(str) %String in lower case

%String Comparison
strcmp('Math','Math')
strcmp('Math','math')
strcmpi('Math','math')
String Functions in MATLAB

%String Concatenation
s1 = 'Linear';
s2 = 'Algebra';
[s1 ' ' s2]

%String Data Type (Modern MATLAB, i.e., R2016b onwards)


% In the context of strings, it mainly refers to using
the string data type (" ") instead of traditional
character arrays (' ').

s = "MATLAB Programming";
strlength(s)

You might also like