This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 03 Rutgers University, Fall 2009 Instructor- Blase E. Ur 1 Functions for Matrices and Vectors 1.1 Max, Min There are also functions to find the maximum and minimum values of a vector or matrix. These are the creatively named max and min functions. For vectors, they return a single scalar with the absolute maximum number. For matrices, they return the maximum or minimum number in each column . Thus, as with the sum and prod functions, to find the absolute largest number in a matrix M , you would use max(max(M)) . The max and min functions can also be used to identify the location of the maximum or minimum number inside a vector (or, with a little bit more trouble, a matrix). If you write a statement like [a,b] = max(V) , where a and b are names of variables and V is a vector, then a will contain the largest value and b will contain the location of that largest value inside the vector V. Note that [a,b] is essentially just a vector of two variables. Also note that if the largest value is found more than one time inside the vector, b will contain the first location where it is found. V = [ 5 7 6 1 2 ]; [a,b] = min(V) a = 1 % The smallest element is 1 b = 4 % 1 is the 4th element of V What if you wanted to find the row and column in which the maximum or minimum is located? We can use a bit of a trick, as in the example below. We could also use the find function, which youll hear about in a second. NOTE, ERROR from last weeks lecture notes FIXED in example below V = [ 5 7; 6 1; 2 3]; [smallC,rowsC] = min(V) smallC = [2 1] % The smallest element IN EACH COLUMN rowsC = [3 2] % The ROWs which contains the smallest elementS [smallO,column] = min(smallC) smallO = 1 % The overall smallest element column = 2 % The COLUMN containing the overall smallest element row = rowsC(column) % Column 2 contains the smallest overall element. % Check which ROW in Column 2 contains the smallest element. row = 2 1.2 Mean, Median The mean and median functions operate on vectors just as youd expect from statistics. The mean cal- culates the average, the sum of the elements of a vector divided by the number of elements. The median identifies the middle number if the vector were to be ordered from smallest to largest element. 1 As with the other matrix functions, calculating the mean or median of a matrix calculates the mean or median of each column. Thus, to find the mean of a matrix M , use mean(mean(M)) . However, this wouldnt work for the median think about why! Instead, use median(M(:)) , which first converts the matrix M to a vector (by making a vector containing ALL of the elements) and then finding the me- dian. NOTE, ERROR from the printed lecture notes FIXED for median(M(:)) in the above paragraph 1.3 Sort, Sortrows You can also sort the elements of a vector in ascending (smallest to largest) order by using the sort func- tion. For matrices, the sort function sorts each column individually.function sorts each column individually....
View Full Document
This note was uploaded on 01/11/2010 for the course 440 127 taught by Professor Blase during the Fall '09 term at Rutgers.
- Fall '09