Arrays

1. Objectives

• To introduce the meaning of a data structure in computing.
• To introduce a fundamental data structure, the array.
• To show different ways that arrays can be created.
• To show different ways that array elements can be extracted.

2. Motivation

• One of the most common ways of organizing a collection of numbers is to place them in an array.
• The array is one of the most common data structures in computing.

3. Priming questions

• Why are vectors and matrices used in mathematics?

4. Notes

4.1. Array definition

• An array is a list of numbers.
• An array is an example of a data structure - it is used to organize a collection of numbers. If you know the structure, you may use a short-hand notation to refer to the entire collection or parts of a collection. For example, if an array named A contains a collection of five numbers 999,1000,1001,1002,1003, I could refer to this collection as simply A instead of saying 999,1000,1001,1002,1003. I could refer to the second and third numbers in the array by writing A([2:3]) instead of writing 1000,1002.
• Another example of a data structure is a matrix.
• MATLAB also has a cell array which allows you to give structure to a collection of arrays.

4.2. Array creation

In MATLAB, there are many ways of creating an array. To create an array with values 1.1, 2.2, 3.3, 4.4, each element in the array can be specified individually

A(1) = 1.1;
A(2) = 2.2;
A(3) = 3.3;
A(4) = 4.4;


Or a short-hand notation can be used:

A = [1.1, 2.2, 3.3, 4.4];


4.3. Accessing array elements

MATLAB uses "one-based" indexing (much like a spreadsheet, for which the first row is labeled 1). One-based indexing means that the first element is accessed with the index 1. If you entered

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A(1)


you would see

A(1) =
1.1


Because MATLAB uses one-based indexing, this statement

A(0) = 99.0


is not allowed. The only allowed values for the index are positive integers (similar to the row numbers that you see on a spreadsheet).

A(1000) = 99.0; % OK.
A(-1) = 99.0; % Error
A(1.1) = 99.0; % Error


4.4. Array example

Previously we did this: Write a MATLAB program that does the following

1. Assigns the decimal number 1.0 to a variable named A1
2. Assigns the value of A1 plus 2.0 to a variable named A2
3. Assigns the value of A2 plus 4.0 to a variable named A3
4. Assigns the value of A3 plus 8.0 to a variable named A4
5. Displays the value of A4

Now do this: Write a MATLAB program that does the following

1. Assigns the decimal number 1.0 to the first element of an array named A.
2. Assigns the value of A(1) plus 2.0 to the second element of an array named A.
3. Assigns the value of A(2) plus 4.0 to the third element of an array named A.
4. Assigns the value of A(3) plus 8.0 to the fourth element of an array named A.
5. Displays the value of A(4).

4.5. Array creation: unspecified values

Suppose you entered

clear B;
B(10) = 10.7


what do you expect for these elements

B(1)
B(2)
B(11)


4.6. The magic colon

Previously, a short-hand notation was used in for loops (Iteration#An_additional_short-hand). Instead of writing

for i = [1,2,3,4,5,6,7,8,9,10]


we could write

for i = [1:10]


This is referred to as using the "magic colon".

You may now recognize that you were already using arrays when you wrote for loops - the [1,2,3,4,5,6,7,8,9,10] has the same syntax as an array.

4.7. Array access: the magic colon

MATLAB allows multiple elements of an array to be accessed or displayed in various ways. To display the second and third elements, you could enter

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A([2,3]) % Best for clarity when number of elements in the access list is short as is here


or

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A([2:3]) % Best for clarity when number of elements in the access list is long


or

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A(2:3)  % Allowed, and sometimes computation will be faster


Important: This is not allowed:

A(2,3)


because it means row 2, column 3 and the array A only has one row. If A were a matrix, then the above statement would be valid.

4.8. Array access: the magic colon

Other examples:

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A([1:5]) % Access elements 1 through 5

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A([1,3,5]) % Access elements 1, 3, and 5

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A([2:6]) % An error because the array only has 5 elements; element 6 was never defined (but it could have been).

A = [1.1, 2.2, 3.3, 4.4, 5.5];
A([0,1]) % An error because A(0) is not defined (and can never be defined).


4.9. Array creation: the magic colon

To create an array in which the numbers follow a pattern such as

A = [1,2,3,4,5,6,7]


you can use the "magic colon" short-hand:

A = [1:1:7]


which means "start at 1, go in steps of 1 until you hit 7". Most of the time you want to go in steps of 1. If you omit the middle number, it is assumed to be 1. So this

A = [1:7]


means the same thing as

A = [1:1:7]


4.10. Array creation: the magic colon

Other examples:

The statement

A = [2,4,6,8]


may be written as

A = [2:2:8]


The statement

A = [10, 8, 6, 4]


may be written as

A = [10:-2:4]


4.11. The magic colon - last element convention

By convention, MATLAB will interpret

A = [2:2:5]


to create an array with elements

A = [2,4]


That is, it interprets the last value to be the largest value that you want in the array, if possible given the requested step size.

4.12. The magic colon - non-integers

The start, stop, and step do not need to be integers. For example,

A = [1.1:3.5:9.0]


is the same as

A = [1.1, 4.6, 8.1]


4.13. The magic colon examples

Write each of the following in long-hand:

A = [10:-2:1]
A = [10:-2:0]
A = [1,2,[7:10],3]


5. Questions

5.1. Data Structure

Why are vectors used in mathematics?

5.2. Syntax I

Describe what will happen when you enter the following on the command line (make sure to account for the semi-colon).

Example:

clear;
A(1) = 1;
A = [3,4,5,6];
A(1)

clear;
A = [2,4,5,6,7];
A(2)+A(3)

clear;
A(1) = 1.0;
A(2) = A(1) + 1.0;
A(3) = A(2) + 1.0;
A(3)

clear;
A(10) = 10;
A


5.3. Syntax II

Describe the problem with each of the following sets of commands

clear;
z = 10;
A(0) = z;

clear;
A(11) = 0;
A(12) = A(13);

clear;
A(-1) = 13;

clear;
1A(1) = 13;


clear;
A(1) = 12;
b = A1+1;

clear;
A = [1:13];
A(14) = A(13) + A(12)


5.4. Short-hand syntax

Which of the following (circle them)

• A = [1:7];
• A = [1:2:7];
• A = [1:7:2];
• A = [1,2,3,4,5,6,7];
• A = [1,3,5,7];
• A = [1,3,[5:2:7]];

is the same as entering

A(1) = 1;
A(2) = 3;
A(3) = 5;
A(4) = 7;


5.5. Array Boundaries

When the following program is entered,

A = [1:100];
b = A(1)
b = A(0)



the result is


b =
1

??? Attempted to access A(0); index must be a positive integer or logical.
Error in ==> untitled at 3
b = A(0)



6. Video Tutorial

6.1. Video

(Note - the video is available in HD - go to full screen mode if fonts are too small)

6.1.1.

Create an array with elements 100, 102, 104, 106, and 108 using three different techniques. When you enter the name of the array on the command line, you should see:

A =

100 102 104 106 108


6.1.2.

When would you not want to use the 3rd technique covered in the video for creating an array?

6.1.3.

What does the following command do?

B = [-100:-10:-200]


6.1.4.

What do you expect to happen when you type the following? Do you agree that the command should not be classified as an error by MATLAB?

C = [1:10:2]


6.1.5.

Do arrays always need to be named with upper-case letters?

6.1.6.

Describe the array that will be created when the following commands are entered:

clear;
D(10) = -10
D(9)  = -9
D(11)

clear;
E = [1:1:10];
E(11) = 13
E(20) = 20