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
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
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
• 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
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.
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
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):');