Cpe 102 Presentationeddited
Cpe 102 Presentationeddited
Using curly braces
e.g >> A = { 3, ‘Three’; 4, ‘Four’; 5, ‘five’}
A=
[3] ’Three’
[4] ’Four’
<date/time>
[5] ’Five’ <footer> 2
STRUCTURE DATA TYPES
Creation of structure using ‘struct keyword’
syntax:
X = struct(field_1, value_1, field_2, value_2, field_3, value_3,..., field_n, value_n)
creates a structure array with multiple fields.
e.g:
Student = struct(‘name’, ‘Jai’, ‘age’, 19, ‘sex’, ‘F’, ‘marks’, [02, 09, 14, 20])
Creation of structure using ‘ dot operator’
syntax: structName.fieldName
e.g X.field_1 or Student.name
<date/time> <footer> 3
STRUCTURE DATA TYPES(Cont’d)
>> Student.name = ‘Jai’;
>> Student.age = 19;
>> Student.sex = ‘F’;
<date/time> <footer> 4
Logical Data type
The logical data type contains ones and zeros for true and false
statements, the size of which is the same 1 byte.
e.g: >> X = [5 4 2 0 -8 0];
>> Y = logical(X)
Y=
111010
The commands returns logical ‘1’ for non-zero (both +ve or -ve)
values and logical ‘0’ for zero values.
<date/time> <footer> 5
Knowing the Datatypes of Data
Check for its type in the workspace window
Type ‘whos variable_name’ at the command prompt
<date/time> <footer> 6
INTER-CONVERSION OF DATATYPES
Using cast function
e.g >> X = uint8(a)
>> Y = cast (X, ‘int8’)
Using Datatype function
e.g: >> str = char(‘Hello MATLAB’)
>> dbl = double(str)
<date/time> <footer> 7
Datatype conversion commands
int2str: converts integer to string
num2str: converts number to string
str2double: converts string to double precision value
bin2dec: converts binary number string to decimal number
dec2bin: converts decimal to binary number in string
<date/time> <footer> 8
MATLAB VARIABLES
A variable is a symbolic name for information or data. The data is
stored in a memory location, the content of which can be changed at
any time.
The name represents the information it contains, but the name itself
is not the information.
The creation of variable is done by using assignment operator ‘=’.
Syntax:
>> variable = expression
<date/time> <footer> 9
Properties of variables
Name
Datatype
Value
Scope
Lifetime of variable
Location (in Memory)
<date/time> <footer> 10
Good variable creation practices
Variable names should be in mixed case starting with lower
case and can as well use underscore to separate part of the
compound name
Variables with a large scope should have meaningful names
Avoid using keywords or special value name for a variable
name.
<date/time> <footer> 11
Creating Variables
In MATLAB, you can create variables to store values. To create a variable,
use the assignment operator (=) and assign a value to a variable name.
Here's an example:
matlab
% Creating variables
x = 10;
y = 5.5;
name = 'John Doe';
<date/time> <footer> 12
Accessing Variable Values
To access the value of a variable, simply refer to it by its name.
For example, you can display the value of x using the disp
function:
matlab
disp(x);
MATLAB will display the value of x in the command window.
<date/time> <footer> 13
Writing Functions
In MATLAB, you can define your own functions to encapsulate a
set of calculations or operations. Here's an example of how to
write a simple function that calculates the area of a rectangle:
matlab
% Function to calculate the area of a rectangle
function area = calculateRectangleArea(length, width)
area = length * width;
end
<date/time> <footer> 14
Calling Functions:
Once you've defined a function, you can call it in your script or command
window. For example, to calculate the area of a rectangle with a length of 5
and width of 3, you can call the calculateRectangleArea function as
follows:
matlab
length = 5;
width = 3;
area = calculateRectangleArea(length, width);
disp(area);
MATLAB will execute the function and assign the calculated area to the variable
area. Then, it will display the value of area in the command window
<date/time> <footer> 15
Function with Multiple Outputs
MATLAB also supports functions with multiple output arguments. Here's
an example:
matlab
% Function to calculate the area and perimeter of a rectangle
function [area, perimeter] = calculateRectangleProperties(length, width)
area = length * width;
perimeter = 2 * (length + width);
end
<date/time> <footer> 16
Calling of functions (c0nt’d)
To call this function and retrieve both outputs, you can do the following:
matlab
length = 5;
width = 3;
[area, perimeter] = calculateRectangleProperties(length, width);
disp(area);
disp(perimeter);
MATLAB will calculate both the area and perimeter and assign the
respective values to the variables area and perimeter. It will then
display both values in the command window.
<date/time> <footer> 17
Matlab mathematical functions
Trigonometric functions:
sin(x): Compute the sine of x (in radians).
cos(x): Compute the cosine of x (in radians).
tan(x): Compute the tangent of x (in radians).
asin(x): Compute the inverse sine (also known as arcsine) of x, returning values
in the range [-π/2, π/2].
acos(x): Compute the inverse cosine (also known as arccosine) of x, returning
values in the range [0, π].
atan(x): Compute the inverse tangent (also known as arctangent) of x, returning
values in the range [-π/2, π/2].
<date/time> <footer> 18
Mathematical functions(cont’d)
Exponential and logarithmic functions:
exp(x): Compute the exponential of x.
log(x): Compute the natural logarithm of x (base e).
log10(x): Compute the common logarithm (base 10) of x.
sqrt(x): Compute the square root of x.
power(x, y): Compute x raised to the power of y.
<date/time> <footer> 19
Mathematical functions(cont’d)
Hyperbolic functions:
sinh(x): Compute the hyperbolic sine of x.
cosh(x): Compute the hyperbolic cosine of x.
tanh(x): Compute the hyperbolic tangent of x.
asinh(x): Compute the inverse hyperbolic sine of x.
acosh(x): Compute the inverse hyperbolic cosine of x.
atanh(x): Compute the inverse hyperbolic tangent of x.
<date/time> <footer> 20
Mathematical functions(cont’d)
Rounding and absolute functions:
round(x): Round to the nearest integer.
floor(x): Round towards negative infinity.
ceil(x): Round towards positive infinity.
abs(x): Compute the absolute value of x.
<date/time> <footer> 21
Mathematical functions(cont’d)
Statistical functions:
mean(x): Compute the mean (average) of the elements in x.
median(x): Compute the median of the elements in x.
std(x): Compute the standard deviation of the elements in
x.
var(x): Compute the variance of the elements in x.
<date/time> <footer> 22
Mathematical functions(cont’d)
Random number generation:
rand(): Generate a random number between 0 and 1.
randn(): Generate a random number from a standard
normal distribution.
randi([a, b]): Generate a random integer between a and
b.
<date/time> <footer> 23