24p. Sums, products, max and min _printable_

24p. Sums, products, max and min _printable_ - Sums,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ©2009 by L. Lagerstrom Sums, Products, Max, and Min • The sum and prod functions • Cumulative sums and products • The max and min functions • Example: Minimizing the cost of a storage tank (see associated video clip for an overview) ©2009 by L. Lagerstrom The sum Function In statistical analysis and other calculations we often have to take the sum or product of a number of values. For example, we might want to sum all the values in a row or column vector. We actually introduced the sum function in an earlier lesson ("Math with arrays"). If we have a row or column vector named x, then sum(x) simply sums up the values in x. If x is a two-dimensional array, you might think that sum(x) would sum all the values in the array. In fact, however, it works a little differently, as the next slide shows. Matlab code Command window display ©2009 by L. Lagerstrom Using the sum Function %Using sum with a row vector x = [3 -2 7 5] result1 = sum(x) %Using sum with a column vector y = [5; 9; -3; 8] result2 = sum(y) %Using sum with an array A = [3 5 -1; 2 4 7; 8 3 2] result3 = sum(A) %Note that, for an array, the sum %function sums each column, not the %whole array. If we want to sum all %the values in the array, we can %simply do the sum function twice: result4 = sum(sum(A)) %The inner sum(A) sums each column %of values in A, and then the second %sum function adds up the three %column sums to give the total sum %for the array values. x = 3 -2 7 5 result1 = 13 y = 5 9 -3 8 result2 = 19 A = 3 5 -1 2 4 7 8 3 2 result3 = 13 12 8 result4 = 33 Matlab code Command window display ©2009 by L. Lagerstrom The prod Function %The prod function works just like %the sum function, except it takes %the product of the values. %Using prod with a row vector x = [3 -2 7] result1 = prod(x) %Using prod with a column vector y = [5; 9; 3] result2 = prod(y) %Using prod with an array A = [3 5 -1; 2 4 7; 8 3 2] result3 = prod(A) %Note again, for an array: the prod %function takes the product of the %elements in each column. If we want %to take the product of all the %values in the array, we do the %prod function twice: result4 = prod(prod(A)); x = 3 -2 7 result1 = -42 y = 5 9 3 result2 = 135 A = 3 5 -1 2 4 7 8 3 2 result3 = 48 60 -14 result4 = 40320
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 ©2009 by L. Lagerstrom Cumulative Sums Sometimes it's useful to do a so-called "cumulative sum." The easiest way to understand the concept is to do a simple example. If x is the row vector [3 6 2 9], then the cumulative sum of x is calculated as follows: [3 3+6 3+6+2 3+6+2+9] which equals [3 9 11 20] In other words, Matlab starts with the first value, then it sums the first two values, then the first three values, then the first four values, and so on until it gets to the end of the row vector. It works similarly for a column vector of values. For an array, Matlab will do a cumulative sum for each column of
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

24p. Sums, products, max and min _printable_ - Sums,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online