Lecture3_Computing
Lecture3_Computing
GET211
Emmanuel Ali
Ayibaemi Ledum Jaafaru Sanusi
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 1 / 25
Outline
1 Introduction
2 Introduction to MATLAB
3 Data Type
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 2 / 25
Data Types
The basic types used to represent numbers. They can be broadly categorized
into two main types: floating-point and integer types.
1. Floating-Point Types These types are used to represent real numbers,
including those with decimal points. They can store very large or very small
numbers due to their ability to handle a wide range of magnitudes.
2. Integer Types These types are used to represent whole numbers (without
fractions). MATLAB provides different sizes of integers, both signed and
unsigned.
Signed Integer Types Signed integers can represent both positive and
negative whole numbers.
Unsigned Integer Types Unsigned integers can only represent non-negative
whole numbers (positive numbers and zero).
These primitive numeric types allow MATLAB to efficiently store and process
numerical data with varying levels of precision and range. The default type
for most numerical computations in MATLAB is double, but for memory
optimization and specific use cases, you can use other numeric types such as
single or integer types (int/uint).
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 3 / 25
Data Description Size Range Example
Type
double Double-precision 64 bits (8 ±1.7×10±308 (15–17 decimal x = 3.14159
floating-point bytes) digits)
(default)
single Single-precision 32 bits (4 ±3.4 × 10±38 (6–9 decimal y = single(3.14159)
floating-point bytes) digits)
int8 8-bit signed inte- 8 bits (1 -128 to 127 x = int8(-50)
ger byte)
int16 16-bit signed in- 16 bits (2 -32,768 to 32,767 x = int16(1000)
teger bytes)
int32 32-bit signed in- 32 bits (4 -2,147,483,648 to x = int32(100000)
teger bytes) 2,147,483,647
int64 64-bit signed in- 64 bits (8 -9,223,372,036,854,775,808 to x = int64(10000000000)
teger bytes) 9,223,372,036,854,775,807
uint8 8-bit unsigned in- 8 bits (1 0 to 255 x = uint8(255)
teger byte)
uint16 16-bit unsigned 16 bits (2 0 to 65,535 x = uint16(60000)
integer bytes)
uint32 32-bit unsigned 32 bits (4 0 to 4,294,967,295 x = uint32(40000000)
integer bytes)
uint64 64-bit unsigned 64 bits (8 0 to x =
integer bytes) 18,446,744,073,709,551,615 uint64(1000000000000)
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 4 / 25
Non-numeric representation
Non-numeric data types are used to store and represent information that is
not strictly numerical, such as text, logical values, complex data structures, or
symbolic expressions.
Text Representation
Character arrays are used to store textual data. In newer versions of
MATLAB, strings are represented by the string data type, which is more
flexible than character arrays (char).
1. Character Arrays (char)
A character array is a sequence of characters, where each character is treated
as an element in an array. It is enclosed in single quotes (’ ’) and is the
traditional way to handle text data in MATLAB.
Example:
charArray = ’ H e l l o , World ! ’ ; % 1 x13 c h a r a c t e r a r r a y
Multiline Character Array Example:
names = [ ’ A l i c e ’ ; ’ Bob ’ ; ’ C a r o l ’ ] ; % C h a r a c t e r a r r a y
2. String Arrays (string)
The string data type is more flexible and easier to work with than char.
String arrays are scalar values, unlike character arrays. They are enclosed in
double quotes (" ").
Example:
s t r = ” H e l l o , World ! ” ; % S t r i n g s c a l a r
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 5 / 25
Feature char string
Syntax Single quotes (’text’) Double quotes
("text")
Array Representa- 2D character arrays 1D/2D string arrays
tion
Padding Require- Requires padding for No padding required
ment alignment
Mutability Modifiable like arrays Immutable
Introduced in Available since early Introduced in R2016b
MATLAB
Table 2: Differences between char and string in MATLAB
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 6 / 25
Operation char Example string Example
Concatenation [’Hi ’ ’there!’] "Hi " + "there!"
Substring str(1:5) extractBetween(str,
1, 5)
Length length(’Hello’) strlength("Hello")
Uppercase upper(’abc’) upper("abc")
Lowercase lower(’ABC’) lower("ABC")
Comparison strcmp(’Hi’, ’hi’) "Hi" == "hi"
Replace strrep(’MAT’, ’A’, replace("MAT",
’I’) "A", "I")
Trim strtrim(’ Hi ’) strtrim(" Hi ")
Convert to char N/A char("MATLAB")
Convert to string string(’MATLAB’) N/A
Check if Empty isempty(’’) strlength("") == 0
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 8 / 25
The following table summarizes the logical operators available in MATLAB, including their
symbols, descriptions, and examples.
Operator Symbol Description Example
AND & Returns true if both result = true &
operands are true. false;
OR | Returns true if at least one result = true |
operand is true. false;
NOT ~ Inverts the value of the result = ~true;
operand.
Exclusive OR xor Returns true if only one result =
operand is true. xor(true,
false);
Equal to == Checks if two values are result = (5 ==
equal. 5);
Not equal to ~= Checks if two values are result = (5 ~=
not equal. 6);
2. Logical Operators
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 10 / 25
Computer Operators
3. Relational Operators
Operator Description Example Result
== Equal to 5 == 5 true
= Not equal to 5=3 true
< Less than 3<5 true
<= Less than or equal to 3 <= 3 true
> Greater than 5>3 true
>= Greater than or equal to 5 >= 5 true
Table 8: Relational Operators
4. Bitwise Operators
Operator Description Example Result
bitand Bitwise AND bitand(6, 3) 2
bitor Bitwise OR bitor(6, 3) 7
bitxor Bitwise XOR bitxor(6, 3) 5
bitnot Bitwise NOT bitnot(6) −7
bitshift Bitwise shift bitshift(3, 2) 12
Table 9: Bitwise Operators
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 11 / 25
Computer Operators
5. Assignment Operators
Operator Description Example
= Assignment a = 5
+= Addition Assignment a += 5
-= Subtraction Assignment a -= 5
*= Multiplication Assignment a *= 5
/= Division Assignment a /= 5
ˆ= Power Assignment a ^= 2
Table 10: Assignment Operators
6. Special Operators
Operator Description Example
: Colon (range/slicing) 1:10
; Statement Separator a = 5; b = 10;
[] Array Concatenation A = [1, 2, 3]
’ Transpose A’
. Element-wise Operator a .* b
Table 11: Special Operators
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 12 / 25
Special Functions for Mathematical Operations
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 13 / 25
Precedence Level Operator Description
1 ( ) Parentheses (for grouping expressions)
2 ., -> Structure field access
3 ’ Transpose
.’ Element-wise transpose
4 ˆ Power
.ˆ Element-wise power
5 +, - Unary plus, unary minus
˜ Logical NOT
6 *, /, \ Multiplication, right division, left divi-
sion
.*, ./, .\ Element-wise multiplication, division
7 +, - Addition, subtraction
8 : Colon operator (range creation)
9 <, <=, >, >= Relational operators
==, ~= Equality, inequality
10 & Element-wise AND
11 | Element-wise OR
12 && Short-circuit AND
13 || Short-circuit OR
14 = Assignment
+=, -=, *=, /=, ^= Compound assignment operators
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 14 / 25
Type Casting in MATLAB
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 15 / 25
Explicit Type Casting
Explicit type casting is when you deliberately convert a value from one data type to
another using a specific function or operation.
- Explicit type casting provides more control over the conversion process. - Implicit type
casting can simplify code but may introduce unintended side effects. - Be mindful of data
type compatibility and potential precision loss during conversions. - Use explicit type
casting when necessary to ensure correct data types and avoid unexpected behavior.
MATLAB provides explicit functions for type casting:
- int8(x): Converts to 8-bit signed integer.
- uint8(x): Converts to 8-bit unsigned integer.
- int16(x): Converts to 16-bit signed integer.
- uint16(x): Converts to 16-bit unsigned integer.
- int32(x): Converts to 32-bit signed integer.
- uint32(x): Converts to 32-bit unsigned integer.
- int64(x): Converts to 64-bit signed integer.
- uint64(x): Converts to 64-bit unsigned integer.
- single(x): Converts to single-precision floating-point number.
- double(x): Converts to double-precision floating-point number.
- logical(x): Converts to logical (true/false) value.
- char(x): Converts to character array.
- cell(x): Converts to cell array.
- struct(x): Converts to structure array.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 16 / 25
Explicit Type casting
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 17 / 25
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 18 / 25
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 19 / 25
Exercise
Create a variable y with the value 10.5. Use isnumeric, isinteger, and
isfloat to check if it is:
- A numeric type.
- An integer type.
- A floating-point type.
Display the results of each check.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 20 / 25
Exercise
Create a double precision variable x with the value 25.6. Then, convert
it to:
- An integer type int32.
- A logical type.
Display the results of the conversions.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 21 / 25
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 22 / 25
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 23 / 25
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 24 / 25
Exercise
Assign the value 19 to the variable num and 4 to div. Calculate the
remainder when num is divided by div using the modulus operator..
Additionally, perform floor division of num by div using the expression
floor(num/div), and compare this result with the standard division
num / div.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester October 28, 2024 25 / 25