Vous êtes sur la page 1sur 6

KWAME NKRUMAH UNIVERSITY OF SCIENCE

AND TECHNOLOGY, KUMASI


COLLEGE OF ENGINEERING
CIVIL ENGINEERING DEPARTMENT

Index Number: 5862816


Course: Computer Programming (CE 257)
Civil Engineering Year 2
LECTURE 1 HOMEWORK ASSIGNMENT
1) >>vec= linspace (-pi, pi, 20);
The linspace function creates a linearly spaced vector; linspace(x,y,n) creates
a vector with n values in the inclusive range of x to y.

2) >>linspace (2,3,6);
A vector (2:0.2:3) will consist of a range of numbers from 2 to 3 with a 0.2
interval between each consecutive number. This will result in 6 numbers.

3) a) >>-5: -1 linspace (-5, -1,5)


b) >>5:2:9 linspace (5,9,3)
c) >>8: -2:4 linspace (8,4,3)

4) >>vec= randi(1,10);
>>vec (1:2: end)
>>vec =randi(1,10);
>>vec (1:2: end);
The expression above will index the odd or even indices for any length of
vector.

5) Size. This is because it tells you both the rows & columns.

6) If you want to know the number of elements, you would use length. If you
want to figure out whether it is a row or column vector, you would use size.

7) >>mat= [1:4; 7:10];


a) >>mat (end, end);
b) >>mat(end);
c) >> [r, c] =size(mat);
>>mat (r, c):

8) >>vec = 1:3:16;
a)>>vec(end);
b)>>vec(numel(vec));
c)>>vec(length(vec));
d)>>v=fliplr(vec);
>>v (1);

9) >>mat=rand (3,5);
>>mat (3, :)= []
The expression mat = rand(3,5) creates a random 3 by 5 matrix mat and
mat(3,:) deletes the 3rd row.

10) mat (2:3, 1:3) = ones (2).


Because the left and right sides are not the same dimensions. The expression on
the left is a 2 by 3 matrix but the expression on the right is a 2 by 2 matrix.

11) >>mat3d= zeros (2,4,3);


>>mat3d(:,:, 2)=1;
>>mat3d(:,:, 3)=5;
mat3d= zeros (2,4,3) creates a 2 x 4 x 3 zero matrix. mat3d(:, :, 2) = 1 changes
the second layer into a 2 x 4 matrix consisting of only ones. Likewise mat3d(:,:,
3) = 5 changes the third layer into a 2 x 4 matrix consisting of only fives.

12) >>num = 3:2:9;


>>denom = 1:4;
>>fracs = num ./ denom;
>>sum(fracs)
The vector num (represents numerator) is a vector from 3 to 9 with an interval of
2 and denom (represents denominator) is vector from 1 to 4. The vector fracs is a
vector consisting of the division between num and denom. The results are then
summed up with sum(fracs).
13) >>mat = randi ([-10 10], 3,5);
a) maximum value in each column
>>max(mat)
b) maximum value in each row
>>max(mat)
c) maximum value for the entire matrix
>>max(max(mat))
d) cumulative maxima
>>cummax(mat)

14)>>ones (3,5) *100;


>>zeros (3,5) +100;

15) >>hours= v (1:2: length(v));


>>payrate = v (2:2: length(v));
>>totalpay = hours*payrate;

16) >>all(r>0 & h>0)


Ans = 1
>>vol = pi.*r.^2.*h

17)>>rainmat = [25 33 29 42; 53 44 40 56];


>>highest = max(max(rainmat));
>> = find(rainmat==highest);

18)>> nums = randi ([50 100], 1, 20);


>>evens = nums(rem(nums,2) == 0); OR >>evens = nums(mod(nums,2) == 0);
>>odds = nums(rem(nums,2) ~= 0); OR >>evens = nums(mod(nums,2) ~= 0);
The vector evens will divide each number in the vector array nums by 2 and
compute the numbers in the array that return a remainder of zero. Likewise, the
vector odds will divide each number in the vector array nums by 2 and
compute the numbers in the array that have remainders not equal to zero.

19) >>vec = [7 5 9 4 -8 -4];


>>v1 = vec (2: end);
>>v2 = vec (1: end-1);
>>v1-v2;

20) >>vec = 1:9;


>>fhalf = vec (1: fix(length(vec). /2);
>>shalf = vec(fix(length(vec). /2) +1: end);

LIKELY EXAM MULTIPLE CHOICE QUESTIONS


1) If a single index is used with a matrix, MATLAB unwinds the matrix
a) row by row b) column by column c) row by column d)column by row
2) The above function in MATLAB is called
a) linear indexing b) subscript indexing c) column wise indexing
d) row wise indexing
3) What is the name of functions used when multiplying or dividing vectors or
matrices of the same size term by term?
a) matrix operands b) matrix operators c) array operators d)
array operands
4) what is the meshgrid function used for?
a) It can specify the x and y coordinates of points in images, or can be used to
calculate functions on two variables x and y.
b) It can return many different types of test matrices for problems.
c) It can be used to find the number of dimensions of an argument.
d) It can be used to create a meshwork of different special matrices for
advanced script formations.
5) If a variable has the dimensions 3 x 4, it could be considered
a) matrix b) row vector c) column vector d) All of the above