Laboratory 2 - Vectors and Arrays
Aim
The aim of this lab is to get you familiar with vectors and arrays in Matlab.
Preparation
Please go through the slides for lectures 6-9. These cover vectors, arrays, functions and strings. As well, read chapters 3, 5 and 6 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
Write matlab programs that perform the following tasks. Make sure that you save your answers in scripts. Run the scripts using the matlab tool. The following questions involve the use of vectors, arrays and strings. Questions with an ∗ are to be tried at the end, if you find the time, or at home.- Construct a vector containing all the odd numbers between 7 and 52. Find the product of all the elements in the vector.
- Construct a vector containing every third number between 19 and 60, in reverse. Now, remove all even numbers from the vector.
- Construct a vector containing 100 evenly spaced values between -20 and 30. Find the sum and mean of all the elements in the vector.
- Construct a vector containing 20 randomly generated numbers between 5 and 10. Find the largest number in the vector. Create a new vector by rounding up all the elements in the vector.
- ∗ Given two vectors of integers of equal length, create a new vector that alternates between the elements of the two vectors. Use only vector operations. Can you generalise this to vectors with unequal lengths?
- Create a 3x3 array A of random numbers. Find the diagonal of A. What is the smallest number in A? Find the product of A with A' and the sum of each row in A.
- Create a two column conversion table that converts the temperature in Celsius to Fahrenheit. The table should have temperatures in the range -10 to 40 degrees Celsius, with increments of 0.5 degree.
- ∗ Given two arrays of integers, of possibly different sizes, write a function that will combine the two arrays, sort them and return a new array of a specified size. If the number of elements in the specified array is larger that the sum of the number of elements in the given two arrays, then the new arrays should be padded with zeroes for the extra elements.
- Write a function that, given two arrays of numbers, returns the sum, mean and median of the numbers in the two arrays.
- Write a function that, given a day of the month in August 2010 as a number, returns the name of the day as a string. Eg, 2 should return 'Monday'.
