Professional Documents
Culture Documents
(b) Evaluate the following MATLAB code and determine the value of X and
Y. Show your work step-by-step.
A = [-1 3 2; -5 2 4; 0 3 4]
B = [0 -3 2; 0 2 0; 0 3 4]
X = A(logical(B))
Y = B(A(:,[2,3])
(5 m)
Name: Matric No: Section:
Q.2 (a) Write a function named LCONV to execute a length conversion from metric
unit (in meter) to U.S. customary unit (in foot, mile, inch). The conversion
rates are given as follow:
1 feet = 0.3048 meter
1 mile = 1609.34 meter
1 inch = 0.0254 meter
This function needs to have a help information. An example of the function
call is;
(b) Write an appropriate MATLAB code to prompt the user to enter the
length value and display the results in 2 decimal places using fprintf
as follows:
(b) Given the following MATLAB code. Change the code from while-loop into an
equivalent for-loop code.
(5 m)
Solution
Q1 (a) As in the flowchart 5
(b)
>> A = [-1 3 2; -5 2 4; 0 3 4] 5
A =
-1 3 2
-5 2 4
0 3 4
>> B = [0 -3 2; 0 2 0; 0 3 4]
B =
0 -3 2
0 2 0
0 3 4
>> X = A(logical(B))
X =
3
2
3
2
4
>> Y = B(A(:,[2,3]))
Y =
0 0
0 -3
0 -3
Q2 (a) 4
function [feet, mile, inch]= LCONV (m)
%this function is used for length conversion
%from meter to foot or mile or inch
feet = m/0.3048;
mile = m/1609.34;
inch = m/0.0254;
end
(b)
m = input('Please enter the length value in meter :');
[feet, mile, inch]= LCONV (m);
fprintf ('%.2f m is equal to %.2f feet\n',m,feet);
fprintf ('%.2f m is equal to %.2f miles\n',m,mile); 6
fprintf ('%.2f m is equal to %.2f inches\n',m,inch);
Q3 (a) 5
A =
1 0
1 1
0 0
0 1
B =
1
1
0
0
C =
0
1
0
1
D =
Name: Matric No: Section:
1
2
0
1
Res =
1 0
0 0
0 0
0 1
(b) 5
str=['M' ,'K', 'A', 'B'; 'T' ,'D' ,'L' ,'F'; 'A' ,'H', 'B', 'J'];
C='';
for i=1:3
for j=1:4
if (j==1||j==3)
C(k)=str(i,j);
end
end
end