KEMBAR78
Arrays and Matlab programming construts.pptx
Unit 3:
Arrays and MATLAB Programming
Constructs
Relational operations
Symbol Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal
to
== Equal to
~= Not equal to
A = [2 4 6; 8 10 12]
A =
2 4 6
8 10 12
B = [5 5 5; 9 9 9]
B=
5 5 5
9 9 9
A < B
ans =
1 1 0
1 0 0
A > 7
ans =
0 0
0
1 1
1
A = 1:3
A =
1 2 3
B = [2; 3]
B =
2
3
A >= B
ans =
0 1 1
0 0 1
Logical (Boolean) Operations
& Find logical AND
Short-Circuit && Logical AND with short-circuiting
~ Find logical NOT
| Find logical OR
Short-Circuit || Logical OR with short-circuiting
xor Find logical exclusive-OR
find Find indices and values of nonzero elements
A = [5 7 0; 0 2 9; 5 0
0]
A =
5 7 0
0 2 9
5 0 0
B = [6 6 0; 1 3 5; -1 0 0]
B =
6 6 0
1 3 5
-1 0 0
A & B
ans =
1 1 0
0 1 1
1 0 0
Short-Circuit AND, &&
Syntax
expr1 && expr2
Examples
b = 1;
a = 20;
x = (b ~= 0) && (a/b >
18.5)
x =
1
or, |
A = [5 7 0; 0 2 9; 5 0
0]
A =
5 7 0
0 2 9
5 0 0
B = [6 6 0; 1 3 5; -1 0 0]
B =
6 6 0
1 3 5
-1 0 0
A | B
ans = 3×3 logical array
1 1 0
1 1 1
1 0 0
Short-Circuit OR, ||
x = 5
if x < 3 || x > 7
disp('x is outside the range of
3 to 7.');
else
disp('x is within the range.');
End
x is within the range.
xor
>> X = [1 0 0]
X =
1 0 0
>> Y = [0 1
1]
Y =
0 1 1
>> xor(X,Y)
ans =
1 1 1
find
X = [1 0 2; 0 1 1; 0 0
4]
X =
1 0 2
0 1 1
0 0 4
k = find(X)
k = 5×1
1
5
7
8
9
If statement
if <expression>
% statement(s) will execute if the expression is true
<statements>
end
a = 10;
% check the condition using if statement
if a < 20
% if condition is true then print the following
fprintf('a is less than 20n' );
end
fprintf('value of a is : %dn', a);
Result:
a is less than 20
value of a is : 10
if...else statement
if <expression>
% statement(s) will execute if the boolean expression
is true
<statement(s)>
else
<statement(s)>
% statement(s) will execute if the boolean expression
is false
end
Example
a = 100;
% check the condition
if a < 20
% if condition is true then print the following
fprintf('a is less than 20n' );
else
% if condition is false then print the following
fprintf('a is not less than 20n' );
end
fprintf('value of a is : %dn', a);
Result:
a is not less than 20
value of a is : 100
Switch Statement
switch
<switch_expression>
case <case_expression>
<statements>
case <case_expression>
<statements>
...
...
otherwise
<statements>
end
Example
grade = 'B’;
switch(grade)
case 'A’
fprintf('Excellent!n' );
case 'B’
fprintf('Well donen' );
case 'C’
fprintf('Well donen' );
case 'D’
fprintf('You passedn' );
case 'F’
fprintf('Better try againn' );
otherwise
fprintf('Invalid graden' );
End
Result:
Well done
If…elseif…else statement
if condition1
% Statements to execute if 'condition1' is true
elseif condition2
% Statements to execute if 'condition2' is true
else
% Statements to execute if no conditions are met
end
Example
score = 85;
if score >= 90
disp('Grade: A');
elseif score >= 80
disp('Grade: B');
elseif score >= 70
disp('Grade: C');
else
disp('Grade: F');
end
• Write a script file that asks the user for the input of a number and
returns the natural logarithm of the number if the number is positive,
and displays an error message otherwise.
• The cost per kilometre for a rental car is Rs.0.50 for the first 100
kilometres, Rs.0.30 for the next 200 kilometres and Rs.0.20 for all
kilometres in excess of 300 kilometres. Write a function that
determines the total cost for a given number of kilometres.
% Script to compute natural logarithm of a positive number
num = input('Enter a number: ');
if num > 0
ln_val = log(num);
fprintf('The natural logarithm of %g is %gn', num, ln_val);
else
fprintf('Error: The input must be a positive number.n');
end
if km <= 100
cost = 0.50 * km;
elseif km <= 300
cost = (0.50 * 100) + (0.30 * (km - 100));
else
cost = (0.50 * 100) + (0.30 * 200) + (0.20 * (km - 300));
end
end
The for Loop
Syntax
for index = values
<program statements>
...
end
values has one of the following
forms −
Sr.No. Format & Description
1
initval:endval
increments the index variable from initval to endval by 1, and repeats execution of program statements until index is
greater than endval.
2
initval:step:endval
increments index by the value step on each iteration, or decrements when step is negative.
3
valArray
creates a column vector index from subsequent columns of array valArray on each iteration. For example, on the first
iteration, index = valArray(:,1). The loop executes for a maximum of n times, where n is the number of columns
of valArray, given by numel(valArray, 1, :). The input valArray can be of any MATLAB data type, including a string, cell
array, or struct.
Example 1
for a = 10:20
fprintf('value of a: %dn', a);
end
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
Example 2
for a = 1.0: -0.1: 0.0
disp(a)
end
1
0.9000
0.8000
0.7000
0.6000
0.5000
0.4000
0.3000
0.2000
0.1000
0
Example 3
for a = [24,18,17,23,28]
disp(a)
end
24
18
17
23
28
The while Loop
Syntax
while <expression>
<statements>
end
a = 10;
% while loop execution
while( a < 20 )
fprintf('value of a: %dn', a);
a = a + 1;
end
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
The Nested Loops
Syntax
for m = 1:j
for n = 1:k
<statements>;
end
end
while <expression1>
while <expression2>
<statements>
end
end
Example
rows = 1:10;
cols = 1:10;
% Initialize an empty matrix to store the results
multiplicationTable = zeros(length(rows),
length(cols));
% Outer loop iterates through rows
for a = rows
% Inner loop iterates through columns
for b = cols
% Calculate the product and store it in the
matrix
multiplicationTable(a, b) = a * b;
end
end
% Display the multiplication table
disp('Multiplication Table (1-10):');
Multiplication Table (1-10):
1 2 3 4 5 6 7 8 9
10
2 4 6 8 10 12 14 16 18
20
3 6 9 12 15 18 21 24 27
30
4 8 12 16 20 24 28 32 36
40
5 10 15 20 25 30 35 40 45
50
6 12 18 24 30 36 42 48 54
60
7 14 21 28 35 42 49 56 63
70
8 16 24 32 40 48 56 64 72
80
9 18 27 36 45 54 63 72 81
90
Arrays and Matlab programming construts.pptx

Arrays and Matlab programming construts.pptx

  • 1.
    Unit 3: Arrays andMATLAB Programming Constructs
  • 2.
    Relational operations Symbol Description <Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to
  • 3.
    A = [24 6; 8 10 12] A = 2 4 6 8 10 12 B = [5 5 5; 9 9 9] B= 5 5 5 9 9 9
  • 4.
    A < B ans= 1 1 0 1 0 0 A > 7 ans = 0 0 0 1 1 1
  • 5.
    A = 1:3 A= 1 2 3 B = [2; 3] B = 2 3 A >= B ans = 0 1 1 0 0 1
  • 6.
    Logical (Boolean) Operations &Find logical AND Short-Circuit && Logical AND with short-circuiting ~ Find logical NOT | Find logical OR Short-Circuit || Logical OR with short-circuiting xor Find logical exclusive-OR find Find indices and values of nonzero elements
  • 7.
    A = [57 0; 0 2 9; 5 0 0] A = 5 7 0 0 2 9 5 0 0 B = [6 6 0; 1 3 5; -1 0 0] B = 6 6 0 1 3 5 -1 0 0 A & B ans = 1 1 0 0 1 1 1 0 0
  • 8.
  • 9.
    Examples b = 1; a= 20; x = (b ~= 0) && (a/b > 18.5) x = 1
  • 10.
    or, | A =[5 7 0; 0 2 9; 5 0 0] A = 5 7 0 0 2 9 5 0 0 B = [6 6 0; 1 3 5; -1 0 0] B = 6 6 0 1 3 5 -1 0 0
  • 11.
    A | B ans= 3×3 logical array 1 1 0 1 1 1 1 0 0
  • 12.
    Short-Circuit OR, || x= 5 if x < 3 || x > 7 disp('x is outside the range of 3 to 7.'); else disp('x is within the range.'); End x is within the range.
  • 13.
    xor >> X =[1 0 0] X = 1 0 0 >> Y = [0 1 1] Y = 0 1 1 >> xor(X,Y) ans = 1 1 1
  • 14.
    find X = [10 2; 0 1 1; 0 0 4] X = 1 0 2 0 1 1 0 0 4 k = find(X) k = 5×1 1 5 7 8 9
  • 15.
    If statement if <expression> %statement(s) will execute if the expression is true <statements> end a = 10; % check the condition using if statement if a < 20 % if condition is true then print the following fprintf('a is less than 20n' ); end fprintf('value of a is : %dn', a); Result: a is less than 20 value of a is : 10
  • 16.
    if...else statement if <expression> %statement(s) will execute if the boolean expression is true <statement(s)> else <statement(s)> % statement(s) will execute if the boolean expression is false end
  • 17.
    Example a = 100; %check the condition if a < 20 % if condition is true then print the following fprintf('a is less than 20n' ); else % if condition is false then print the following fprintf('a is not less than 20n' ); end fprintf('value of a is : %dn', a); Result: a is not less than 20 value of a is : 100
  • 18.
    Switch Statement switch <switch_expression> case <case_expression> <statements> case<case_expression> <statements> ... ... otherwise <statements> end
  • 19.
    Example grade = 'B’; switch(grade) case'A’ fprintf('Excellent!n' ); case 'B’ fprintf('Well donen' ); case 'C’ fprintf('Well donen' ); case 'D’ fprintf('You passedn' ); case 'F’ fprintf('Better try againn' ); otherwise fprintf('Invalid graden' ); End Result: Well done
  • 20.
    If…elseif…else statement if condition1 %Statements to execute if 'condition1' is true elseif condition2 % Statements to execute if 'condition2' is true else % Statements to execute if no conditions are met end
  • 21.
    Example score = 85; ifscore >= 90 disp('Grade: A'); elseif score >= 80 disp('Grade: B'); elseif score >= 70 disp('Grade: C'); else disp('Grade: F'); end
  • 24.
    • Write ascript file that asks the user for the input of a number and returns the natural logarithm of the number if the number is positive, and displays an error message otherwise. • The cost per kilometre for a rental car is Rs.0.50 for the first 100 kilometres, Rs.0.30 for the next 200 kilometres and Rs.0.20 for all kilometres in excess of 300 kilometres. Write a function that determines the total cost for a given number of kilometres.
  • 25.
    % Script tocompute natural logarithm of a positive number num = input('Enter a number: '); if num > 0 ln_val = log(num); fprintf('The natural logarithm of %g is %gn', num, ln_val); else fprintf('Error: The input must be a positive number.n'); end
  • 26.
    if km <=100 cost = 0.50 * km; elseif km <= 300 cost = (0.50 * 100) + (0.30 * (km - 100)); else cost = (0.50 * 100) + (0.30 * 200) + (0.20 * (km - 300)); end end
  • 27.
    The for Loop Syntax forindex = values <program statements> ... end
  • 28.
    values has oneof the following forms − Sr.No. Format & Description 1 initval:endval increments the index variable from initval to endval by 1, and repeats execution of program statements until index is greater than endval. 2 initval:step:endval increments index by the value step on each iteration, or decrements when step is negative. 3 valArray creates a column vector index from subsequent columns of array valArray on each iteration. For example, on the first iteration, index = valArray(:,1). The loop executes for a maximum of n times, where n is the number of columns of valArray, given by numel(valArray, 1, :). The input valArray can be of any MATLAB data type, including a string, cell array, or struct.
  • 29.
    Example 1 for a= 10:20 fprintf('value of a: %dn', a); end
  • 30.
    value of a:10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 value of a: 20
  • 31.
    Example 2 for a= 1.0: -0.1: 0.0 disp(a) end
  • 32.
  • 33.
    Example 3 for a= [24,18,17,23,28] disp(a) end
  • 34.
  • 35.
    The while Loop Syntax while<expression> <statements> end
  • 36.
    a = 10; %while loop execution while( a < 20 ) fprintf('value of a: %dn', a); a = a + 1; end
  • 37.
    value of a:10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 38.
    The Nested Loops Syntax form = 1:j for n = 1:k <statements>; end end
  • 39.
  • 40.
    Example rows = 1:10; cols= 1:10; % Initialize an empty matrix to store the results multiplicationTable = zeros(length(rows), length(cols)); % Outer loop iterates through rows for a = rows % Inner loop iterates through columns for b = cols % Calculate the product and store it in the matrix multiplicationTable(a, b) = a * b; end end % Display the multiplication table disp('Multiplication Table (1-10):');
  • 41.
    Multiplication Table (1-10): 12 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90