CHAPTER 8

Functions that Operate on Arrays

8.1 The sum and cumsum Functions
8.2 The prod and cumprod Functions
8.3 The max and min Functions
8.4 The sort Function
8.5 The mean and median Functions
8.6 Moments and the Standard Deviation Function: std
8.7 The diff Function and an Approximate Derivative



There are a whole arsenal of function in MATLAB that will be useful in your work. We will discuss and demonstrate the following functions in this chapter.

Function

Performs on vectors

sum

Sums the elements

cumsum

Gives a running sum of the elements

prod

Multiplies the elements together

cumprod

Gives a running product of the elements

max

Finds the maximum value

min

Finds the minimum value

sort

Sorts the elements

mean

Finds the mean value of the elements

std

Finds the standard deviation of elements

diff

Gives the differences between adjacent elements


All of these also perform the same operation on each column of a matrix. Let's look at each function in a MATLAB session. We start it by setting up some test vectors and matrices. We will include some complex arrays as well as real ones.

>> vr=[3 -3 -5.2 12.4]; 
>> vc=vr+i*[-2 3 -1 2.2]; 
>> mr=[3 14 -5 
      -3 15 -2 
     -5.2 1 10 
     12.4 16 11]; 

Now we will look at the way each function treats these arrays.


8.1 The sum and cumsum Functions

Here is a test of the sum and cumsum functions.

>> disp(sum(vr)) 
    7.2000 
>> disp(sum(vc)) 
   7.2000 + 2.2000i 
>> disp(sum(mr)) 
   7.2000   46.0000   14.0000 

The sum function just sums the elements in both real and complex vectors. It sums each column in a matrix and gives a vector of these sums. If we want to sum over rows, we need to transpose the matrix first and transpose the result if we want a single column result.

 >> disp(sum(mr)) 
   12.0000 
   10.0000 
    5.8000 
   39.4000 

Now that we have seen what the sum function does, it is not too hard to figure out what cumsum gives:

>> disp(cumsum(vr)) 
3.0000         0   -5.2000    7.2000 
>> disp(cumsum(vc)) 
3.0000 - 2.0000i    0 + 1.0000i  -5.2000   7.2000 + 2.2000i 
>> disp(cumsum(mr)) 
    3.0000   14.0000   -5.0000 
         0   29.0000   -7.0000 
   -5.2000   30.0000    3.0000 
    7.2000   46.0000   14.0000  

The result is always an array of the same size as the array operated on. The last element is always what sum gave. The rest are partial sums over 1, then 2 then 3, etc. elements in the vector or columns of a matrix. For example, operating on the real vector: vr, we get:

  3                       as the sum of the first element  
  3+(-3)=0                as the sum of the first two elements 
  3+(-3)+(-5.2)=-5.2      as the sum of the first three elements  
  3+(-3)+(-5.2)+12.4=7.2  as the sum all four elements 


These four results are then placed in a vector result.


8.2 The prod and cumprod Functions


Next the prod and cumprod functions will be tested. The prod function acts just like sum except the elements are multiplied together:

>> disp(prod(vr)) 
  580.3200 
>> disp(prod(vc)) 
  5.4444e+02 - 8.6268e+02i 
>> disp(prod(mr)) 
   1.0e+03 * 
    0.5803    3.3600    1.1000 

The complex product may not be familiar, but the other results should not be surprising. You can check the two real cases with a calculator. You will find that the product over the columns of the matrix were displayed in short format mode and truncation shows only

0.5803 * 10^3 or 580.3

rather than 580.32.

The cumulative product function behaves in an analogous fashion to the cumulativesum function:

>> disp(cumprod(vr)) 
    3.0000   -9.0000   46.8000  580.3200 
>> disp(cumprod(vc)) 
   1.0e+02 * 
   0.0300 - 0.0200i -0.0300 + 0.1500i 0.3060 - 0.7500i 5.4444 - 8.6268i 
>> disp(cumprod(mr)) 
   1.0e+03 * 
    0.0030    0.0140   -0.0050 
   -0.0090    0.2100    0.0100 
    0.0468    0.2100    0.1000 
    0.5803    3.3600    1.1000 

We will leave it to the exercises to figure out these results.


8.3 The max and min Functions


The max and min functions find the largest and smallest elements in a vector or each column of a matrix. This is done in the algebraic sense and if the elements are complex, the magnitude of complex numbers is used in the comparison. Here is what our example arrays have for maxima:

>> disp(max(vr)) 
   12.4000 
>> disp(max(vc)) 
  12.4000 + 2.2000i 
>> disp(max(mr)) 
   12.4000   16.0000   11.0000 

The real arrays again offer no surprises, but the example chosen for the complex vector fails to tell us exactly what the criteria was since the last element had both the largest real part and largest magnitude. This can be easily remedied:

>> vc(3)=-5.2-12.4*i; 
>> disp(max(vc)) 
  -5.2000 -12.4000i 
>> disp(vc) 
   3.0000 - 2.0000i  -3.0000 + 3.0000i  -5.2000 -12.4000i  12.4000 + 2.2000i
>> disp(abs(vc)) 
    3.6056    4.2426   13.4462   12.5936 

Thus we see that the criterion is based on the magnitude of the numbers.

Now the min function is seen to give results based on the same procedures used in the max function except we now look for the smallest elements or those with the smallest magnitudes.

>> disp(min(vr)) 
   -5.2000 
>> disp(min(vc)) 
   3.0000 - 2.0000i 
>> disp(min(mr)) 
   -5.2000    1.0000   -5.0000 

There were no questions about what min would find, were there?


8.4 The sort Function


Here is a function that goes along with the last pair. It gives the complete order for all the elements in a vector or in each column of a matrix. Again the magnitudes of complex numbers are used in the comparison. The ordering is done from smallest to largest.

>> disp(sort(vr)) 
   -5.2000   -3.0000    3.0000   12.4000 
>> disp(sort(vc)) 
   3.0000 - 2.0000i  -3.0000 + 3.0000i  12.4000 + 2.2000i  -5.2000 -12.4000i 
>> disp(sort(mr)) 
   -5.2000    1.0000   -5.0000 
   -3.0000   14.0000   -2.0000 
    3.0000   15.0000   10.0000 
   12.4000   16.0000   11.0000 

If we want to sort the matrix over rows, we need to do a little transposing:

>> disp(sort(mr')') 
   -5.0000    3.0000   14.0000 
   -3.0000   -2.0000   15.0000 
   -5.2000    1.0000   10.0000 
   11.0000   12.4000   16.0000 

If we want to know where the elements in the sorted form stood in the original array we can use sort in the form:

>> m=[2 5 8 
     -1 7 5 
      0 2 10]; 

 

>> [ms,mi]=sort(m) 
ms = 
    -1     2     5 
     0     5     8 
     2     7    10 
mi = 
     2     3     2 
     3     1     1 
     1     2     3 


8.5 The mean and median Functions
Now let's look at the difference between the mean and median of our arrays. Here are the mean values:   >> disp(mean(vr)) 1.8000 >> disp(mean(vc)) 1.8000 - 2.3000i >> disp(mean(mr)) 1.8000 11.5000 3.5000

In each case MATLAB simply adds the elements and divides by the number of elements. We could have found the same result for the vectors by:

>> disp(sum(vr)/length(vr)) 
1.8000 >>
disp(sum(vc)/length(vc)) 1.8000 - 2.3000i

The matrix mean could also have been found as long as we use the length to get its number of rows:

>> disp(sum(mr)/length(mr)) 
    1.8000   11.5000    3.5000 

Now look at the median values applied to vectors with three elements:

>> disp(median([-1 5 6])) 
     5 
>> disp(median([-1 -1 6])) 
    -1 

It simply orders the elements and reports the middle one. In our example vectors, we have an even number of elements. Traditionally, we would expect to order them and find the mean of the two in the middle.

>> disp(median(vr)) 
     0 
>> disp(median(vc)) 
  4.7000 + 2.6000i 

Looking back at the sorted vectors, we see that the mean of the middle elements is what is returned by the median function.


8.6 Moments and the Standard Deviation Function: std


Let us now look at a single vector: x of n elements:

x1, x2, .... xn


The mean will be designated as: xmean. The kth moment about the mean is defined as:

		 i=n
	 µk =[sum(xi -xmean )k ]/n 
		 i=1


The standard deviation is then defined as:

s = µ2


The std function gives this value. Let's check to see that the definitions given above are consistent with what we expect:

>> x=[.1 1 1.2 .5 .8 .9]; 
>> xbar=mean(x) 
xbar = 
    0.7500 
>> xmxb=x-xbar 
xmxb = 
   -0.6500  0.2500  0.4500 -0.2500  0.0500  0.1500 
>> n=length(x) 
n = 
     6 
>> mu2=sum(xmxb.^2)/n 
mu2 = 
    0.1292 
>> sqrt(mu2) 
ans = 
    0.3594 
>> std(x) 
ans = 
    0.3594 

It checks!


8.7 The diff Function and an Approximate Derivative

We will demonstrate the diff function only for vectors. It can be used on matrices and gives the same thing for each column of the matrix that is does for a vector. For a vector with elements:

x1, x2, .... xn


diff gives a vector with elements:

x2 - x1, x3 - x2, .... xn - xn-1


If we apply diff to the vector:

>> x 
x = 
    0.1000    1.0000    1.2000    0.5000    0.8000    0.9000 

we get:

>> disp(diff(x)) 
    0.9000    0.2000   -0.7000    0.3000    0.1000 

As an application of diff look at calculating the derivative of the function sin(t) using the numerical approximation:

df/dt at tn ~ (f(tn+1) - f(tn))/(tn+1 - tn)

Here is a session used to do just that:

>> t=0:pi/32:pi; 
>> sint=sin(t); 
>> dsint=diff(sint); 
>> dt=diff(t); 
>> dfdt=dsint./dt; 

The length of each of the vectors: sint, dsint, dfdt is:

>> disp(length(dfdt)) 
    32 

Thus we will make our comparison over the same number of values of t:

>> t=t(1:32); 

 

>> plot(t,[dfdt;cos(t)]) 
>> title('Numerical Approximation to dsin(t)/dt') 
>> xlabel('t') 
>> text(1.5,.5,'___ Appx. with dt = pi/32') 
>> text(1.5,.4,'--- cos(t)') 

Here is what we got:


Figure 8.1 Numerical Approximation to a Derivative


Continue on to Chapter 9
Return to Table of Contents