0% found this document useful (0 votes)
19 views42 pages

Day 2

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)
19 views42 pages

Day 2

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/ 42

Day 2

Prof. Soma Das


Department of Computer Science and Engineering
Institute of Engineering & Management, Kolkata
MATLAB Variable
MATLAB Variables
MATLAB Variables
MATLAB Datatypes
• Numeric Types - Integer and floating-point data
• Characters and Strings - Text in character arrays and string
arrays
• Dates and Time - Arrays of date and time values that can be
displayed in different formats
• Categorical Arrays - Arrays of qualitative data with values from
a finite set of discrete, nonnumeric data
• Tables - Arrays in tabular form whose named columns can have
different types
• Timetables - Time-stamped data in tabular form
MATLAB Datatypes
• Structures - Arrays with named fields that can contain data of varying types
and sizes
• Cell Arrays - Arrays that can contain data of varying types and sizes
• Function Handles - Variables that allow you to invoke a function indirectly
• Map Containers - Objects with keys that index to values, where keys need
not be integers
• Time Series - Data vectors sampled over time
• Data Type Identification - Determine data type of a variable
• Data Type Conversion - Convert between numeric arrays, strings and
character arrays, dates and times, cell arrays, structures, or tables
Numeric Types

• Numeric classes in MATLAB® include signed and


unsigned integers, and single-precision and double-
precision floating-point numbers. By default, MATLAB
stores all numeric values as double-precision floating
point. (You cannot change the default type and precision.)
You can choose to store any number, or array of
numbers, as integers or as single-precision. Integer and
single precision arrays offer more memory-efficient
storage than double precision.
• All numeric types support basic array operations, such as
indexing, reshaping, and mathematical operations.
Double
• double is the default
numeric data type
(class) in MATLAB®,
providing sufficient
precision for most
computational tasks.
Numeric variables are
automatically stored as
64-bit (8-byte) double-
precision floating-
point values.
Double- Syntax of creation
Knowing the class
What is single and double precision?

• Single-precision floating-point format uses 32 bits of


computer memory and can represent a wide range of
numerical values. Often referred to as FP32, this
format is best used for calculations that won’t suffer
from a bit of approximation.
• Double-precision floating-point format, on the other
hand, occupies 64 bits of computer memory and is
far more accurate than the single-precision format.
Double precision vs Single precision

• Use the realmin and


realmax functions
to display the
minimum and
maximum positives
values that can be
represented in
double precision.
Double precision vs Single precision

• Display the
minimum and
maximum
positive values
that can be
represented in
single precision.
Cast
• B = cast(A,newclass) returns the data in A converted to
the data type (class) newclass, where newclass is the name
of a built-in data type compatible with A. Any values in A
that are outside the range of newclass are truncated in B
to the nearest endpoint.

• When converting a floating-point number to an integer,


the cast function rounds the number to the nearest
integer. If the floating-point number has a fractional part
of exactly 0.5, then it rounds away from zero to the
integer with larger magnitude.
Cast - Example

• Convert int8
values to uint8. -
Define a vector
of 8-bit integers.
Convert a to
unsigned 8-bit
integers. The –5
value outside the
range of uint8 is
truncated to 0.
Convert vector to existing datatype
Convert matrix to existing datatype

• Create a 2-by-3 matrix of zeros whose elements are


32-bit unsigned integers.
• Create a 2-by-2 sparse matrix of the data type
double.
• Convert A to the same data type and sparsity as the
variable p.
Convert matrix to existing datatype
Typecast

• Convert data type without changing underlying data


• Y = typecast(X,type) converts the bit patterns of X
to the data type specified by type without changing
the underlying data. X must be a full, noncomplex,
numeric scalar or vector.
Typecast
Query Type and Value
• allfinite - Determine if all array elements are finite
• anynan - Determine if any array element is NaN
• isinteger - Determine whether input is integer array
• isfloat - Determine if input is floating-point array
• isnumeric - Determine whether input is numeric array
• isreal - Determine whether array uses complex storage
• isfinite - Determine which array elements are finite
• isinf - Determine which array elements are infinite
• isnan - Determine which array elements are NaN
isNaN

• TF = isnan(A) returns a logical array containing 1


(true) where the elements of A are NaN, and 0 (false)
where they are not. If A contains complex numbers,
isnan(A) contains 1 for elements with either real or
imaginary part is NaN, and 0 for elements where
both real and imaginary parts are not NaN.
Example

• Create a row
vector and
determine
which
elements are
NaN.
Steps to replace NaN values

• Create an array and find the elements with NaN


values.

• Index into A with TF to access the elements of A


that are NaN. Replace the NaN values with 0.
Before replacement
After replacement
Task

• Explore all the query types and set example for each
Reference -
https://in.mathworks.com/help/matlab/numeric-
types.html
Numeric value limits
• eps - Floating-point relative accuracy
• flintmax - Largest consecutive integer in floating-point format
• Inf - Create array of all Inf values
• intmax - Largest value of specific integer type
• intmin - Smallest value of specific integer type
• NaN - Create array of all NaN values
• realmax - Largest positive floating-point number
• realmin - Smallest normalized floating-point number
Characters and Strings
Character arrays and string arrays provide storage for text data in
MATLAB®.

• A character array is a sequence of characters, just as a numeric


array is a sequence of numbers. A typical use is to store short
pieces of text as character vectors, such as c = 'Hello World'.

• A string array is a container for pieces of text. String arrays


provide a set of functions for working with text as data. You
can create strings using double quotes, such as str = "Greetings
friend". To convert data to string arrays, use the string function.
String arrays

• string - String array


• strings - Create string array with no characters
• join - Combine strings
• plus - Add numbers, append strings
String
• 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.

• 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.
Creation

• You can create a string scalar by enclosing a piece of


text in double quotes
• To create a string array, you can concatenate string
scalars using square brackets, just as you can
concatenate numbers into a numeric array.
Split string and find unique word
• To find the unique words in a string, split it on space characters
and call the unique function.

• First, create a string scalar.


• Remove the exclamation point.
• Convert all letters in str to lowercase characters.
• Split str on space characters using the split function. split
discards the space characters and returns the result as a string
array.
• Find the unique words in str using the unique function.
Output
Task

• Split the following string and find Unique words


“Oh My God!!! Tell me the midsem examination is not
taking place this month. Please !! It will be a disaster if the
midsem examination takes place this month”
• Show output of each step
• Copy paste the string given in the slide. Was there any
word which got repeated? If yes why? How can you avoid
that repeatation?
Size of string
Task

• Find the length of the following string “'Is there a


way we can postpone the exam?'”
Convert cell array

• Convert a cell array of character vectors to a string


array.
• To access the second element in the first row of str,
index using smooth parentheses. You can access
strings in a string array with matrix indexing, just as
you would access elements of a numeric array.
Example
Convert Numeric Array
Task

• Explain what difference will be displayed by a


Numeric array and it’s corresponding String array
• Do a vice –versa of this operation i.e take a create a
string array in which each element represents a
number and then convert it to a numeric array.
[Hint: use the double function]

You might also like