Ex4 : Introduction to Matlab Programming
This exercise will help us start working with matlab. By this exercise you will teach yourselves the basics of Matlab.
In class we will continue to learn together more advanced commands and applications.
You should do this exercise on your own. Deadline : Wednesday - May 26 (Until our class, at 11:00)
How to start ?
Use one of the many excellent tutorials that are on the web. This tutorial is recommended.
There is also a Matlab guide in Hebrew here.
Each command in Matlab has clear documentation, you can see it by typing "help" or "doc" with the name of the command after it.
The purpose of the following question is to make sure you are familiar with the basic concepts and functions in Matlab.
Your code should be simple and efficient. In general, when working in Matlab always prefer matrix operations (multiplications and such) over loops.
You can open Matlab from your shell by simply writing matlab.
Write a short Matlab function named ex4 which does the following:
- Create a vector of ones, of size 10, and print it to the screen (note if you omit the ";" at the end of a command, the result will be printed to the screen)
- Creates a ROW vector of size 100 with all even numbers from 2 to 200
- Print to the screen the numbers at index 15-20 in this vector
- Creates a COLUMN vector of size 100 with all odd numbers from 1 to 199. Save both vectors to a matlab file named dat.mat (use the command save)
- Receive the name of a matlab file as a parameter to this function (file ending with .mat), you can assume this file contains a single matrix variable named M. upload the matrix from the file (check out the command load)
- Check if the matrix is square (.. the size of the matrix), if not print the error message "Mat not square!" and exit
- Check if the matrix is the identity matrix, if so print the message "Identity!", and create a square random matrix the size of the input matrix, with values between 0-100, and continue working with it (the command rand can help you here)
- Create a plot containing two subplots, side by side as following: (check the commands plot, subplot,xlabel, ylabel, title,hist,sum)
- The left subplot should be a scatter plot of two vectors, x and y, where x is a vector representing the sum of the rows of the matrix, and y is the sum of the columns of the matrix. Give this plot a title of your choice, and labels on X and Y axis
- The right subplot, is a histogram with 5 bins of all the values on the diagonal of the matrix (which command should you use here?)
- And now let's do some math :
- Multiply each element in the matrix by the value in the same cell in the transposed matrix (note the difference between "*" and ".*")
- Multiply the RESULTING matrix by a column vector of the correct size, whith entries containing sequential numbers from 1,2,3,4...etc
- Find the maximal value of the RESULTING vector. This should be the return value of your function!
What to submit ?
Submit your code in a file named ex4.m (same as the name of your function)
Good Luck!