Arrays
From CDS 130
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 shorthand notation to refer to the entire collection or parts of a collection. For example, if an array named
A
contains a collection of five numbers999,1000,1001,1002,1003
, I could refer to this collection as simplyA
instead of saying999,1000,1001,1002,1003
. I could refer to the second and third numbers in the array by writingA([2:3])
instead of writing1000,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 shorthand notation can be used:
A = [1.1, 2.2, 3.3, 4.4];
Note 

Other notation for creating arrays is allowed (but not recommended). For example, adding whitespace around the parentheses is allowed: A ( 1 ) = 1.1; A (2) = 2.2; A (3) =3.3; A (4 ) = 4.4; as is omitting the spaces after the comma: A = [1.1,2.2,3.3,4.4,5.5]; In fact, the comma is optional if replaced with a space (but it is a bad habit to omit them!): A = [1.1 2.2 3.3 4.4 5.5]; 
4.3. Accessing array elements
MATLAB uses "onebased" indexing (much like a spreadsheet, for which the first row is labeled 1
). Onebased 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 onebased 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
Note 

A "zerobased" indexing program would use the index 
4.4. Array example
Previously we did this: Write a MATLAB program that does the following


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


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 shorthand notation was used in for
loops (Iteration#An_additional_shorthand). 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" shorthand:
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  nonintegers
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 longhand:
A = [10:2:1] A = [10:2:0] A = [1,2,[7:10],3]
Answer 

A = [10:2:1] is the same as A = [10, 8, 6, 4, 2] A = [10:2:0] is the same as A = [10, 8, 6, 4, 2] A = [1,2,[7:10],3] is the same as A = [1,2,7,8,9,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 semicolon).
Example:
clear; A(1) = 1; A = [3,4,5,6]; A(1)
Answer 

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

The value 
clear; A(1) = 1.0; A(2) = A(1) + 1.0; A(3) = A(2) + 1.0; A(3)
Answer 

The third line results in 
clear; A(10) = 10; A
Answer 

Something like 0 0 0 0 0 0 0 0 0 10 will be displayed. The command 
5.3. Syntax II
Describe the problem with each of the following sets of commands
clear; z = 10; A(0) = z;
Answer 

The first element of an array is 
clear; A(11) = 0; A(12) = A(13);
Answer 

The command 
clear; A(1) = 13;
Answer 

The first element of an array is labeled with 
clear; 1A(1) = 13;
Answer 

Variable names may not start with a number. 
clear; A(1) = 12; b = A1+1;
Answer 


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

The command 
5.4. Shorthand 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 uppercase 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