Arrays

Array Functions

Function Description
sum(Z) adds all elements of array Z
prod(Z) multiplies all elements of array Z
length(Z) returns the length of array Z
[Zmax, i] = max(Z) returns largest element Zmax of Z and its position, i
[Zmin, i] = min(Z) returns smallest element Zmin of Z and its position, i
sort(Z) sorts elements of array Z in ascending order
find(Z>3) finds the indices of elements of array Z larger than 3

 

Exercise

> Create a script M-file called arrayFunctions.m to run the following code, starting by entering comment headers with your name, the date and the purpose of the M-file.

  1. Create an array of 23 random numbers between 0 and 200 called randy using the following command:
    randy = 200*rand(1,23)
  2. Find the average of the numbers in randy using the sum and length commands
  3. Find the largest element and the smallest element in randy and swap them
  4. Sort randy into ascending order. Do you know how to sort it in descending order?
  5. Find the indices of elements in randy which are larger than 100.