0% found this document useful (0 votes)
4 views

3 - MATLAB - Vector, Matrix, Array, and Cell

Uploaded by

Souvik Saha
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 views

3 - MATLAB - Vector, Matrix, Array, and Cell

Uploaded by

Souvik Saha
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
You are on page 1/ 36

Vector, Matrix, Array,

and Cell
Dr. Susovan Jana
Assistant Professor & Assistant HOD
Department of Computer Science & Engineering (IoT)
Institute of Engineering & Management, Kolkata, INDIA
Email (O): susovan.jana@iem.edu.in
Email (P): jana.susovan2@gmail.com
2
Vector in MATLAB
❑ A vector is a one-dimensional array of numbers.
❑ MATLAB allows creating two types of vectors −
─ Row vectors
─ Example: x=[2 5 7 3]
─ Column vectors
─ Example: x=[2
5
7
3]

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
3
Vector Creation in MATLAB
❑ Syntax for Row Vector
─ x=[<values separated by one space or comma>]
─ x=[2 5 7 3]
─ x=[2, 5, 7, 3]
❑ Syntax for Column Vector
─ x=[<values separated by semi-colon>]
─ x=[2; 5; 7; 3]

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
4
Vector Creation
❑ x = j:k
─ x = 1:10
─ Create a unit-spaced vector of
numbers between 1 and 10.
The colon operator uses a
default increment of +1.
❑ x = j:i:k
─ x = 1:2:10
─ X=10:-2:1
─ Create a vector whose
elements incremented or
decremented by 2.

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
5
Matrices and Arrays
❑ All MATLAB variables are multidimensional arrays, no
matter what type of data.
❑ A matrix is a two-dimensional array often used for
linear algebra.

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
6
Matrix Creation
❑ Matrix creation with zeros
& ones
─ x=zeros(3)
─ x=zeros(3,2)
─ x=ones(3)
─ x=ones(3,2)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
7
Matrix Creation
❑ Matrix with integer values
─ a=[1 3 6; 2 5 8; 4 7 9]
─ b=[1 2 3; 4 5 6; 7 8 9]
❑ Matrix with complex number
─ c = [3+4i, 4+3j; -i, 10j]

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
8
Concatenating two Matrix
❑ Horizontally
─ x=[a, b]
❑ Vertically
─ x=[a; b]

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
9
Accessing Values from Matrix
❑ Indexing starts from 1 in MATLAB
❑ Syntax
─ <array name>(<row index>, <column index>)
─ <array name>(<starting row index> : <ending row index> , <starting
column index> : <ending column index> )
─ <array name>(<row index>, <starting column index> : <ending
column index> )
─ <array name>(<starting row index> : <ending row index> , <column
index> )
─ <array name>(<row index>, : )
─ <array name>( : , <column index> )

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
10
Access Single Value
❑ Syntax
─ <array name>(<row index>, <column index>)
❑ Example
─ a(2, 3)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
11
Access 2D Array from a 2D Array
❑ Syntax
─ <array name>(<starting row index> : <ending row index> ,
<starting column index> : <ending column index> )
❑ Example
─ a(1 : 2, 2 : 3)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
12
Access Values from a Single Row/Column
❑ Syntax
1. <array name>(<row index>,
<starting column index> : <ending
column index> )
2. <array name>(<starting row index>
: <ending row index> , <column
index> )
❑ Example
1. a(3, 1:2)
2. a(1:2, 3)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
13
Access a Complete Row/Column
❑ Syntax
1. <array name>(<row index>, : )
2. <array name>( : , <column index> )
❑ Example
1. a(2, : )
2. a( : , 2)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
14
Modifying Values in Matrix
❑ Syntax
─ <array name>(<row index>, <column index>)
❑ Example
─ a (1:2, 1:2)=[11 13; 12 15]

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
15
Deletion of any row & column
❑ Delete any row
─ Syntax: <matrix>(<row position>, : )
─ Example: x( 2 , : ) = []
❑ Delete any column
─ Syntax: <matrix>(: , <column position>)
─ Example: x( : , 5 ) = []

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
16
Addition on Matrix
❑ Syntax
1. <value> + <matrix name>
2. <matrix name> + <matrix name>
❑ Example
1. 5 + a
2. a + b

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
17
Subtraction on Matrix
❑ Syntax
1. <matrix name> - <value>
2. <matrix name> - <matrix name>
❑ Example
1. a - 5
2. a - b

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
18
Multiplication on Matrix
❑ Syntax
1. <value> * <matrix name>
or
<value> .* <matrix name>
2. <matrix name 1> .* <matrix name 2>
3. <matrix name 1> * <matrix name 2>
❑ Example
1. 5 * a
or
2. 5 .* a
3. a .* b
4. a * b

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
19
Division on Matrix
❑ Syntax
1. <matrix name> / <value>
or
<matrix name> ./ <value>
2. <matrix name 1> ./ <matrix name 2>
3. <matrix name 1> / <matrix name 2>
❑ Example
1. a / 2
or
a ./2
2. a ./ b
3. a / b

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
20
String Array
❑ You can represent text in MATLAB® using string
arrays.
❑ Each element of a string array stores a sequence of
characters.
❑ The sequences can have different lengths without
padding, such as "yes" and "no".
❑ A string array that has only one element is also called
a string scalar.

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
21
String Array
❑ You can index into, reshape, and concatenate string
arrays using standard array operations, and you can
append text to them using the + operator.
❑ If a string array represents numbers, then you can
convert it to a numeric array using the double
function.

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
22
String Creation
❑ Syntax
─ <name> = “<text>”
❑ Example
─ str = "A horse! A horse! My kingdom for a horse!“
❑ Get length by calling strlength function

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
23
Character Array Creation
❑ Syntax
─ <array name>=‘<text>’
❑ Example
─ ca='My name is Mr. Xyz.‘
❑ Get size using size function
─ size(ca)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
24
Convert All Letters in String to
Lowercase Characters
❑ Syntax
─ lower(<string name>)
❑ Example
─ lower(str)
❑ For uppercase call
upper in same way

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
25
Remove Something from String
❑ Syntax
─ erase(<string name>, “<character to erase>")
❑ Example
─ erase(str,"!")

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
26
Split String on Space Characters
❑ It discards spaces and returns
a string array
─ split(<string name>)
❑ Split by specified delimiter
─ split(<string name>,<delimiter>)
❑ Example
─ split(str)
❑ Join
─ join(ans)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
27
Find the Unique Words in String
❑ Syntax
─ unique(<string name>)
❑ Example
─ unique(str)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
28
Conversion to String Array
❑ Syntax
─ string(<name of variable>)
❑ Example
─ st=string(A)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
29
Convert Strings That Represent
Numbers
❑ Syntax
─ double(<name of string array>)
❑ Example
─ double(str)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
30
Cell Array
❑ A cell array is a data type with indexed data
containers called cells, where each cell can contain
any type of data.
❑ Cell arrays commonly contain either lists of text,
combinations of text and numbers, or numeric arrays
of different sizes.
❑ Refer to sets of cells by enclosing indices in smooth
parentheses, ().
❑ Access the contents of cells by indexing with curly
braces, {}.
Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
31
Creation of Cell Array
❑ When you have data to put into a cell array, create
the array using the cell array construction operator,
{}.
❑ C = {1,2,3; ‘Aditi', rand(5,10,2), {11; 22; 33}}

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
32
Create a Blank Cell Array
❑ Syntax
─ <name>=cell(<size 1>, …., <size n>)
❑ Example
─ C=cell(2)
─ C=cell(2, 3)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
33
Assign Value to Cell Array
❑ Syntax
─ <cell name>(<row index>,<column index>)={<value>}
❑ Example
─ C(1, 1)={'MATLAB'}
─ C(2, : )={[2 5 3], ‘Rahul’, 25.3}
─ C(1, 2 : 3 )={[2 5; 4 3], ‘Aditi’}

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
34
Access Cells
❑ Syntax
─ <cell name>=(<row index>, <column index>)
─ <cell name>={<row index>, <column index>}
❑ Example
─ C(1, 2)
─ C{1, 2}

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
35
Cell to Matrix
❑ C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}
❑ A=cell2mat(C)

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA
36

Dr. Susovan Jana, Department of Computer Science & Engineering (IoT), IEM, Kolkata, INDIA

You might also like