MATLAB problems
(for reviewing the course content and preparing for the exam)
Exercise 1
What is the Matlab command to create the column vector x which holds the integers: 2,5,8,11,…,89?
Exercise 2
Write the Matlab statement(s) to set to 5 all the elements of the vector vec having even indexes (i.e., v(2), v(4), etc..).
Exercise 3
Create in Matlab a vector A1×5, the matrices B4×2, C2×3 and D2×3, using the function rand(). Then construct the matrix E5×5 of the form
Exercise 4
Write a Matlab script to plot three functions on thee same graph. Plot y = sin(x) in red, y = sin(2x) in black, and y = sin(3x) in green, using 200 x coordinates, equally distributed between −4 and 8. Set the limits of Y axis between −1.5 and 1.5.
Exercise 5
The density function of a normal distribution with mean m and standard devi-
A
B
C
D
ation s is defined as
1 1 (x−m )2 f(x)=√e2 s
s 2π
Write a Matlab function density which receives three input arguments (x, m, s) and returns the value of f.
1
Exercise 6
Write a Matlab function that receives as input parameters two matrices of the same size, A and B, and return the matrix C where C(i, j) = max{A(i, j), B(i, j)}. Use the for-loop statement in the function body.
Exercise 7
Write a function sum2 which receives as input a positive integer n and returns
twonumbersS1andS2,whereS1=kandS2= k=1 k=1
function body avoid, if possible, the for-loop statement. Exercise 8
Write a function sign percent which receives as input a matrix A and return the percentage of the positive elements and the percentage of negative elements.
Exercise 9
Write a function called checkpositive that receives a 2D matrix A as input and returns the row vector b. The element b(i) will be set to 1 if the row i of the matrix A contains only strictly positive values, and set to 0 if not. The function should also report the results to the command window; the report should look something like this:
Row 1: no
Row 2: yes
…etc.
Calling example
b = checkpositive([4 1 1; -1 0 0; 0 0 0; .1 .2 .1])
Row 1: yes
Row 2: no
Row 3: no
Row 4: yes b=
[1 0 0 1]
Exercise 10
Write a function mymean which receives as input a vector x and calculate the mean value of the elements of x, but excluding the minimum and the maximum values. Hint: you may use the built-in functions min() and max(). Calling example:
2
n n2k−5
k3 +k2 +4
. Inthe
x = [2, 1, 0, 0, 4, 3, 4]; m = mymean(x)
m=
2
Exercise 11
The vector volume contains the sales volume realised each month from January 2001 to December 2010. In the following you may use the built-in function mean.
• Calculate the mean of sales for all January months.
• Write a function mean month which receives as input parameters the vector volume and an integer number n between 1 and 12, and returns the mean of sales for the month number n over all years.
• Calculate the mean of sales for the year 2005.
• Write a function mean year which receives as input parameters the vector volume and an integer number n between 1 and 10, and returns the mean of sales for the year 2000 + n.
• Calculate the vector myear representing the mean of sales for each year (the annual mean).
• Using the built-in functions min and max, calculate the number of elements from volume being either greater than the maximum annual mean or lower than the minimum annual mean.
Exercise 12
Write a function which receives as input argument a vector vec and return two output arguments: the maximum value from vec and the index of the corresponding element (if there are several such elements, return the first index found). You must use a for-loop statement.
Exercise 13
The Matlab function ismember(x, A) returns true if x is one of the elements of matrix A and false otherwise. Write a function check sudoku which receives as input a matrix A of size 9 × 9 and returns true if each integer number between 1 and 9 appears only once in each row of A and each column of A (otherwise, the function returns false).
Exercise 14
Consider a vector patient of structures, where each element is a structure with the fields:
3
name
a cell array of strings, the first cell containing the first name, the
second cell the second name age integer number
gender one of the strings ’F’ or ’M’
bp a vector of 10 elements representing blood pressure measurements
• Display (using fprintf) the second name of all male patients having more than 35 years.
• Calculate the number of patients between 25 and 45 years old, and having the mean of blood pressure measurements greater than 108. (Hint: you may use built-in function mean())
4