Laboratory 3 - Functions, Cell Arrays and Structures, Matrices and Recursion
Aim
The aim of this lab is to get you familiar with functions, cell arrays and structures, matrices and recursion in Matlab.
Preparation
Please go through the slides for lectures 10-12. These cover cell arrays and structures, matrices and recursion. As well, read chapters 7, 9 and 12 of the textbook. Try out the examples covered in the lectures and the text book.
Tools
The only tool you will need is the matlab programming environment. To use this, you will need to logon to a computer in one of the Computer Science labs, open up a terminal window and type the following from the command line:
matlab
Instructions
The following questions involve the use of cell arrays and structures. Questions with an ∗ are to be tried at the end, if you find the time, or at home.- Create a cell array that holds your name, id, degree and year in degree (2 for 2nd year, and so on). Print this information.
- ∗ Write a function that will return the number of vectors, strings and boolean values in a cell array that holds vectors, strings and boolean values.
-
A used car company holds all of its used car records in an array
of structures. Each structure in the array holds a particular used
car's details and has the following fields:
- Make: A string representing the make of the car.
- Year: A number that corresponds to the year.
- Price: A number that holds the marked price of the car.
- Kilometers: The number of kilometers clocked.
- Accidents: The number of accidents that the car has been in.
- Construct two matrices of integers, A with dimensions 3x3 and B with dimensions 3x1. Find the product AxB. Also find the product A.*B(2). What happens when you try A.*B or B*A?
- Get the second row of A. Get the third column of A. Interchange the second and third row of A. Arrange the columns of A in reverse order (col 3, col 2, col 1).
- Construct the 3x3 identity matrix I3. Find I3*A.
- Find the eigen values and eigen vectors of A.
- Construct a 3x3 matrix of integers, C. Try A.*C, A*C and C*A. Now do A./C, A/C, A\C, C\A and inv(A).
- ∗
Find the intersection of the following two lines:
-
y = 3x - 2
y = 5x + 7
- ∗
Create row vectors X1 of size 4 with values from 0 to 3 and X2 of
size 3 with values from 2 to 4. Now, create a meshgrid
using X1 and X1. Read the matlab documentation on meshgrid. Use
the following command:
-
[Y1 Y2] = meshgrid(X1,X2)
- Write a function that computes the sum of the elements of a vector using recursion.
- Write a recursive function that takes in a number and returns the product of the odd numbers between that number and 1.
- ∗ Write a recursive function that takes in a vector and returns the element with the maximum value and the index of that element as two separate return values.
