KEMBAR78
Matlab-Data types and operators | PDF
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Matlab - Data Types and Operators
Luckshay Batra
luckybatra17@gmail.com
2018
Luckshay Batra 2018 Matlab - Data Types and Operators 1 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Outline I
Introduction
Data Types
Vectors
The colon operator
Indexing
Matrices
Multi-dimensional matrices
Structures
Where structures are useful
Text Strings
sprintf
Cell Arrays
Numeric Precision
Operators
Arithmetic Operators
Luckshay Batra 2018 Matlab - Data Types and Operators 2 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Outline II
Relational Operators
Logical Operators
Bitwise Operations
References
Luckshay Batra 2018 Matlab - Data Types and Operators 3 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Data Types I
Matlab knows the following data types:
Matrices Matrices of floating point numbers. Vectors and
scalars are special cases.
Strings Text strings. These are really vectors of characters to
Matlab.
Structures
Cell Cell arrays.
Then there are more specialized datatypes such as tables . You
can also define your own data types. The type of a variable is not
fixed. The following is perfectly fine code (and a popular source of
errors).
a = 1;
a = ‘a string’;
Luckshay Batra 2018 Matlab - Data Types and Operators 4 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
• MATLAB does not require any type declaration or dimension
statements. Whenever MATLAB encounters a new variable
name, it creates the variable and allocates appropriate
memory space.
• If the variable already exists, then MATLAB replaces the
original content with new content and allocates new storage
space, where necessary.
For example
Total = 42
The above statement creates a 1-by-1 matrix named ’Total’ and
stores the value 42 in it.
Luckshay Batra 2018 Matlab - Data Types and Operators 5 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Vectors
To create a vector, simply fill it with values:
a = [1, 2, 3];
disp(a);
1 2 3
Matlab knows row and column vectors:
b = [1; 2; 3];
disp(b);
1
2
3
disp(a*b);
14
Luckshay Batra 2018 Matlab - Data Types and Operators 6 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
The colon operator
The colon : makes a vector of sequential numbers:
disp(1 : 4);
1 2 3 4
Now with step size 3
disp(1 : 3 : 10);
1 4 7 10
Luckshay Batra 2018 Matlab - Data Types and Operators 7 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Indexing I
To extract elements from a vector, hand it a list (vector) of
indices.
>>a = [2, 3, 4, 5];
>>disp(a[1, 3]);
2 4
The vector of indices can be constructed with the colon operator:
a = 11 : 20;
disp(a(2 : 2 : 6));
12 14 16
Any vector can be used to index elements:
Luckshay Batra 2018 Matlab - Data Types and Operators 8 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Indexing II
idxV = [2 5 8];
disp(a(idxV));
12 15 18
Then there is logical indexing.
>>a = 11 : 20;
idxV = (a >15);
>>disp(idxV)
0 0 0 0 0 1 1 1 1 1
>>disp(a(idxV))
16 17 18 19 20
Luckshay Batra 2018 Matlab - Data Types and Operators 9 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Matrices I
A matrix is an n-dimensional array of numbers. One can also have
arrays of other data types.
To create a matrix, simply fill it with values.
a = [1 2 3; 4 5 6];
disp(a);
1 2 3
4 5 6
Many commands work directly on matrices.
Luckshay Batra 2018 Matlab - Data Types and Operators 10 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Matrices II
a = [1 2 3];
b = [2; 1; 1];
disp(a * b);
7
disp(b * a);
2 4 6
1 2 3
1 2 3
To extract elements:
c = b*a;
disp(c(1:2, 2:3));
4 6
2 3
Luckshay Batra 2018 Matlab - Data Types and Operators 11 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Matrices III
To extract all elements:
disp(c(1,:));
2 4 6
But note c(:) yields all elements flattened into a vector!
disp(c(:));
2 1 1 4 2 2 6 3 3
To extract a list of elements, use sub2ind .
Luckshay Batra 2018 Matlab - Data Types and Operators 12 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Matrices IV
c = [1 2 3; 4 5 6];
idxV = sub2ind(size(c), [1,2], [2,3]);
>>c(idxV)
ans =
2 6
>>disp([c(1,2), c(2,3)])
2 6
Luckshay Batra 2018 Matlab - Data Types and Operators 13 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Multi-dimensional matrices
Matlab matrices can have more than 2 dimensions.
a = rand([3,2,5]);
size(a)
ans =
3 2 5
a(:,:,3)
ans =
0.9218 0.4057
0.7382 0.9355
0.1763 0.9169
Sub-matrices work just like ordinary 2-dimensional matrices. But
a(:,1,:) is not a 2D matrix. Its a 3D matrix with a singleton 2nd
dimension.
Luckshay Batra 2018 Matlab - Data Types and Operators 14 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Structures
Structures are containers for variables of different types.
They are defined by simply adding element to a blank structure.
Example : Store the contact information for a
person
contactS.Name = ’John Doe’;
contactS.Age = 37;
The elements are accessed by name:
disp(contactS.Age);
37
One can have structure arrays, but they are tricky because each
element must be a structure with the same fields.
Often, cell arrays are more useful.
Luckshay Batra 2018 Matlab - Data Types and Operators 15 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Where structures are useful
Use a structure to pass a large number of arguments to a function.
Luckshay Batra 2018 Matlab - Data Types and Operators 16 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Where structures are useful
Use a structure to pass a large number of arguments to a function.
• Set of parameters and prices for solving a household problem.
Luckshay Batra 2018 Matlab - Data Types and Operators 16 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Where structures are useful
Use a structure to pass a large number of arguments to a function.
• Set of parameters and prices for solving a household problem.
• Our models store all fixed parameters in a structure that is
passed to essentially all functions.
Luckshay Batra 2018 Matlab - Data Types and Operators 16 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Where structures are useful
Use a structure to pass a large number of arguments to a function.
• Set of parameters and prices for solving a household problem.
• Our models store all fixed parameters in a structure that is
passed to essentially all functions.
Structures can make code robust against changes.
Luckshay Batra 2018 Matlab - Data Types and Operators 16 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Where structures are useful
Use a structure to pass a large number of arguments to a function.
• Set of parameters and prices for solving a household problem.
• Our models store all fixed parameters in a structure that is
passed to essentially all functions.
Structures can make code robust against changes.
• Add a preference parameter to the model. Only the household
code needs to be changed. Other programs use the same
structure to look up model parameters.
Luckshay Batra 2018 Matlab - Data Types and Operators 16 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Text Strings
To Matlab, a text string is a vector of characters. And a character
is represented by a number.
Therefore, most vector operations work on text strings.
Example
myName = ’Hendricks’;
disp(myName(5:8));
rick
disp(myName(5) == ’r’);
1
Luckshay Batra 2018 Matlab - Data Types and Operators 17 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
sprintf
sprintf produces formatted text from (usually) numerical inputs.
The syntax is almost the same as in C .
Example
sprintf(’Integer: % i. Float: %5.2f. String:
%s’, 5, 3.71, ’test’)
ans =
Integer: 5. Float: 3.71. String: test
Luckshay Batra 2018 Matlab - Data Types and Operators 18 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Cell Arrays
A cell array is an n-dimensional array of mixed data.
Each cell can hold a different data type.
Example
<<x = {’abc’, 17; [3,4,5], {1, 2}}
x =
’abc’ [ 17]
[1 × 3 double] {1×2 cell}
<<disp(x{1,1})
abc
<<disp(x{2,1})
3 4 5
Most Common Use: replacement for structure array when one is
not sure that all elements have the same fields.
Luckshay Batra 2018 Matlab - Data Types and Operators 19 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Numeric Precision I
By default, numeric variables are stored as double (double
precision float, 64 bit).
Even if you define x=1 , it is a double .
<<x=1;
<<class(x)
ans =
double
If you work with large (simulated) datasets, you may want to store
matrices in formats that take less storage.
>>x = ones([1,3], ’uint8’)
x =
1 1 1
Luckshay Batra 2018 Matlab - Data Types and Operators 20 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Numeric Precision II
This leads to some nice opportunities for errors about which
Matlab helpfully does not complain (another Matlab bug).
>>x(2)=1e5
x =
1 255 1
The number assigned to x was too large for an 8 bit integer. It
gets truncated without warning.
Worse, applying common numeric operations to integers returns
integers:
Luckshay Batra 2018 Matlab - Data Types and Operators 21 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Numeric Precision III
>>y = x/2
y =
1 128 1
>>class(y)
ans =
uint8
Rule of thumb: Store large datsets in low precision. Make
everything double the moment you load it.
Luckshay Batra 2018 Matlab - Data Types and Operators 22 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. MATLAB is designed to
operate primarily on whole matrices and arrays. Therefore,
operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations
Luckshay Batra 2018 Matlab - Data Types and Operators 23 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. MATLAB is designed to
operate primarily on whole matrices and arrays. Therefore,
operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations
Luckshay Batra 2018 Matlab - Data Types and Operators 23 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. MATLAB is designed to
operate primarily on whole matrices and arrays. Therefore,
operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations
Luckshay Batra 2018 Matlab - Data Types and Operators 23 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. MATLAB is designed to
operate primarily on whole matrices and arrays. Therefore,
operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations
Luckshay Batra 2018 Matlab - Data Types and Operators 23 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. MATLAB is designed to
operate primarily on whole matrices and arrays. Therefore,
operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations
Luckshay Batra 2018 Matlab - Data Types and Operators 23 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators
MATLAB allows two different types of arithmetic operations:
Matrix arithmetic operations same as defined in linear algebra.
Array operations are executed element by element,
both on one-dimensional and multidimensional array.
Array arithmetic operations The matrix operators and array
operators are differentiated by the period (.) symbol.
However, as the addition and subtraction operation is
same for matrices and arrays, the operator is same
for both cases.
Luckshay Batra 2018 Matlab - Data Types and Operators 24 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators I
The following table gives brief description of the operators:
Operator: +
Addition or unary plus. A+B adds the values stored in
variables A and B. A and B must have the same size,
unless one is a scalar. A scalar can be added to a
matrix of any size.
Operator: -
Subtraction or unary minus. A-B subtracts
the value of B from A. A and B must have
the same size, unless one is a scalar. A
scalar can be subtracted from a matrix of
any size.
Luckshay Batra 2018 Matlab - Data Types and Operators 25 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators II
Operator: *
Matrix multiplication. C = A*B is the linear
algebraic product of the matrices A and B.
Operator: .*
Array multiplication. A.*B is the
element-by-element product of the arrays A
and B. A and B must have the same size,
unless one of them is a scalar.
Luckshay Batra 2018 Matlab - Data Types and Operators 26 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators III
Operator: /
Slash or matrix right division. B/A is roughly the
same as B*inv(A). More precisely, B/A =
(A’B’)’.
Operator: ./
Array right division. A./B is the matrix
with elements A(i,j)/B(i,j). A and B must
have the same size, unless one of them is a
scalar.
Luckshay Batra 2018 Matlab - Data Types and Operators 27 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators IV
Operator: .
Array left division. A.B is the matrix with elements
B(i,j)/A(i,j). A and B must have the same size,
unless one of them is a scalar.
Luckshay Batra 2018 Matlab - Data Types and Operators 28 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators V
Operator: 
Backslash or matrix left division. If A is a
square matrix, AB is roughly the same as
inv(A)*B, except it is computed in a
different way. If A is an n-by-n matrix and
B is a column vector with n components,
or a matrix with several such columns, then
X = AB is the solution to the equation
AX = B. A warning message is displayed if
A is badly scaled or nearly singular.
Luckshay Batra 2018 Matlab - Data Types and Operators 29 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators VI
Operator: ˆ
Matrix power. Xˆp is X to the power p, if p
is a scalar. If p is an integer, the power is
computed by repeated squaring. If the
integer is negative, X is inverted first. For
other values of p, the calculation involves
eigenvalues and eigenvectors, such that if
[V,D] = eig(X), then Xˆp = V*D.ˆp/V.
Operator: .ˆ
Array power. A.ˆB is the matrix with
elements A(i,j) to the B(i,j) power. A and
B must have the same size, unless one of
them is a scalar.
Luckshay Batra 2018 Matlab - Data Types and Operators 30 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Arithmetic Operators VII
Operator: ’
Matrix transpose. A’ is the linear algebraic
transpose of A. For complex matrices, this
is the complex conjugate transpose.
Operator: .’
Array transpose. A.’ is the array transpose of A. For
complex matrices, this does not involve conjugation.
Luckshay Batra 2018 Matlab - Data Types and Operators 31 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
Relational operators can also work on both scalar and non-scalar
data. Relational operators for arrays perform element-by-element
comparisons between two arrays and return a logical array of the
same size, with elements set to logical 1 (true) where the relation
is true and elements set to logical 0 (false) where it is not.
Luckshay Batra 2018 Matlab - Data Types and Operators 32 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
The following table shows the relational operators available in MATLAB: 1 2 3
4 5 6
1 <
2 <=
3 >
4 >=
5 ==
6 =
• Less than
• Less than or equal to
• Greater than
• Greater than or equal to
• Equal to
• Not equal to
Luckshay Batra 2018 Matlab - Data Types and Operators 33 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
The following table shows the relational operators available in MATLAB: 1 2 3
4 5 6
1 <
2 <=
3 >
4 >=
5 ==
6 =
• Less than
• Less than or equal to
• Greater than
• Greater than or equal to
• Equal to
• Not equal to
Luckshay Batra 2018 Matlab - Data Types and Operators 33 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
The following table shows the relational operators available in MATLAB: 1 2 3
4 5 6
1 <
2 <=
3 >
4 >=
5 ==
6 =
• Less than
• Less than or equal to
• Greater than
• Greater than or equal to
• Equal to
• Not equal to
Luckshay Batra 2018 Matlab - Data Types and Operators 33 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
The following table shows the relational operators available in MATLAB: 1 2 3
4 5 6
1 <
2 <=
3 >
4 >=
5 ==
6 =
• Less than
• Less than or equal to
• Greater than
• Greater than or equal to
• Equal to
• Not equal to
Luckshay Batra 2018 Matlab - Data Types and Operators 33 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
The following table shows the relational operators available in MATLAB: 1 2 3
4 5 6
1 <
2 <=
3 >
4 >=
5 ==
6 =
• Less than
• Less than or equal to
• Greater than
• Greater than or equal to
• Equal to
• Not equal to
Luckshay Batra 2018 Matlab - Data Types and Operators 33 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Relational Operators
The following table shows the relational operators available in MATLAB: 1 2 3
4 5 6
1 <
2 <=
3 >
4 >=
5 ==
6 =
• Less than
• Less than or equal to
• Greater than
• Greater than or equal to
• Equal to
• Not equal to
Luckshay Batra 2018 Matlab - Data Types and Operators 33 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Logical Operators
MATLAB offers two types of logical operators and functions: 1 2
Element-wise
These operators operate on corresponding elements of logical
arrays.
1 Element-wise logical operators operate element-by-element on
logical arrays. The symbols &,|, and are the logical array
operators AND, OR, and NOT.
2 Short-circuit logical operators allow short-circuiting on logical
operations. The symbols && and are the logical
short-circuit operators AND and OR.
Luckshay Batra 2018 Matlab - Data Types and Operators 34 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Logical Operators
MATLAB offers two types of logical operators and functions: 1 2
Short-circuit
These operators operate on scalar and logical expressions.
1 Element-wise logical operators operate element-by-element on
logical arrays. The symbols &,|, and are the logical array
operators AND, OR, and NOT.
2 Short-circuit logical operators allow short-circuiting on logical
operations. The symbols && and are the logical
short-circuit operators AND and OR.
Luckshay Batra 2018 Matlab - Data Types and Operators 34 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Bitwise Operations
Bitwise operators work on bits and perform bit-by-bit operation.
p q p&q p|q pˆq
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Table: The truth tables for &, |, and ˆ
Luckshay Batra 2018 Matlab - Data Types and Operators 35 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
References
• http://matlab-
introduction.readthedocs.io/en/latest/matlab datatypes.html
•
https://www.tutorialspoint.com/matlab/matlab data types.html
•
https://www.tutorialspoint.com/matlab/matlab operators.html
Luckshay Batra 2018 Matlab - Data Types and Operators 36 / 37
Introduction
Data Types
Operators
References
luckybatra17@gmail.com
Thank You!
Luckshay Batra 2018 Matlab - Data Types and Operators 37 / 37

Matlab-Data types and operators