(a) What is the value of y if y=length(linspace(0.1,0.5))? Give a short explanat
ID: 2247542 • Letter: #
Question
(a) What is the value of y if y=length(linspace(0.1,0.5))? Give a short explanation how you get your answer.
(b) Assume matrix B exists in the workspace containing a number of rows and columns.
i. Write a single MATLAB statement to assign the elements of the last column to variable lastColumn.
ii. Using your answer in part(i) above, write a single MATLAB statement to assign the number of elements in a variable lastColumn to a variable called numElems.
iii. Write a series of MATLAB statements to determine the average of all elements in matrix B which are multiples of 6 (without using loop structures) and assign the answer
to variable avgdiv6.
(c) Write a series of MATLAB statements to determine and display (using fprintf function) how many odd numbers exists in matrix B. Format of display should be:
There are ……..odd numbers in matrix B.
(d) Write a series of MATLAB statements to display the elements of column 2 in matrix B in descending order
(e) Write a series of MATLAB statements to calculate how many numbers in matrix B which are between 12 and 65 (all inclusive) and assign the answer to variable numInRange.
Explanation / Answer
a) Answer
The statement linspace(0.1, 0.5); generates 100 linearly spaced vectors between 0.1 and 0.5 and then the statement y = length( linspace(0.1, 0.5)); assigns the length (size) of vector generated i.e., 100 to the statement y. Hence y = 100
b) Answer
(i) lastColumn = B(:, end)
(ii) numElems = lastColumn
(iii) div6 = B(rem(B(1:end), 6)==0)
avgdiv6 = sum(div6) / numel(div6)
( c ) Answer
numOdd = length(B(rem(B(1:end), 2)~=0));
fprintf('There are %d odd numbers in matrix B. ', numOdd)
( d ) Answer
sortedB = sort(B(:, 2), 'descend')
( e ) Answer
for i = 1:row
for j = 1:col
if B(i, j) >= 12 && B(i, j) <= 64
numInRange(ii) = B(i, j)
ii = ii + 1;
end
end
end
clc;
x = (linspace(0.1,0.5));
B = [1:5; 10:14; 12, 24, 48, 0, 36]
C = B(:, end);
div6 = B(rem(B(1:end), 6)==0)
avgdiv6 = sum(div6) / numel(div6)
numOdd = length(B(rem(B(1:end), 2)~=0));
fprintf('There are %d odd numbers in matrix B. ', numOdd)
sortedB = sort(B(:, 2), 'descend')
[row, col] = size(B);
numInRange = 0;
ii = 1;
for i = 1:row
for j = 1:col
if B(i, j) >= 12 && B(i, j) <= 64
numInRange(ii) = B(i, j)
ii = ii + 1;
end
end
end
numInRange
B =
1 2 3 4 5
10 11 12 13 14
12 24 48 0 36
div6 =
12 24 12 48 0 36
avgdiv6 =
22
There are 5 odd numbers in matrix B.
sortedB =
24
11
2
numInRange =
12
numInRange =
12 13
numInRange =
12 13 14
numInRange =
12 13 14 12
numInRange =
12 13 14 12 24
numInRange =
12 13 14 12 24 48
numInRange =
12 13 14 12 24 48 36
numInRange =
12 13 14 12 24 48 36
>>
Complete Matlab Code involving above logic as an exampleclc;
x = (linspace(0.1,0.5));
B = [1:5; 10:14; 12, 24, 48, 0, 36]
C = B(:, end);
div6 = B(rem(B(1:end), 6)==0)
avgdiv6 = sum(div6) / numel(div6)
numOdd = length(B(rem(B(1:end), 2)~=0));
fprintf('There are %d odd numbers in matrix B. ', numOdd)
sortedB = sort(B(:, 2), 'descend')
[row, col] = size(B);
numInRange = 0;
ii = 1;
for i = 1:row
for j = 1:col
if B(i, j) >= 12 && B(i, j) <= 64
numInRange(ii) = B(i, j)
ii = ii + 1;
end
end
end
numInRange
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.