Practical 3: Arrays

Arrays

Creating Arrays

Try these different ways of creating arrays

> Type in the elements of the array one-by-one at the prompt within square brackets. Elements should be separated by commas or spaces (advantage: easy for small set of numbers, disadvantage: tedious and time consuming for lots of numbers).

x1=[2, 5, 13, -1, 0.111, pi]


> Create an array of integers with increment 1 (by default) from smallest to largest (advantage: fast, especially for many numbers, disadvantage: increment of 1 only)

smallest= -3;

largest= 24;

x2= [smallest:largest]

The following is also valid:

x2= [-3:24]

> You can also specify the increment or the step size, h. Check the following (note you must define smallest and largest first). (advantage: allows different increments, disadvantage: one must calculate number of elements).

h= 0.06

x3= [smallest:h:largest]

> You can specify the number of points instead of the step size using the linspace command.

points= 20;

x4= linspace(smallest,largest,points)