30
Arrays 1 Multiple values per variable

Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect

Embed Size (px)

Citation preview

Arrays 1

Multiple values per variable

Why arrays?

Can you collect one value from the user?How about two?Twenty?Two hundred?How about…I need to collect data from the user and be able to work with the individual pieces afterwards. I don’t know how much data there will be. How do I store this incoming information?

1. Creating scalars

• Assign a value to a variable (i.e. Hardcode)pressure = 10; % Pascalstemperature = 298; % kelvin

2.1. row vector

• Square brackets [] and commas

• OR square brackets and white space

1 by 4

1 by 4

2.1. column vector

• Square brackets and semi-colons

• Or a row vector transposed with the apostrophe

3 by 1

3 by 1

New operators…

[] Use [] to tell MATLAB you are about to hardcode an array, Use a comma or white-space to create new columns; Use semi-colons to create a new row’ Use an apostrophe to transpose the array

(caution if you are using imaginary numbers)

NO MATTER WHAT YOU DO:

ALWAYS KEEP THE ARRAY RECTANGULAR!!!!!

3. Creating Matrices

• Simply a combination of all operators introduced with vectors!– Square brackets [ ]– Spaces or commas , ,– Semi-colons ;– Apostrophes ‘

• Just keep in mind:only RECTANGULAR matrices

X

3.1. Matrices: hard-coding

• Use semi-colons to create new rows.

• Good or bad? Why?

2 by 3 3 by 2

2. Creating vectors

• There are LOTS of ways to create vectors, based on three simple ideas:

– The values in the vector are pre-defined. For example: [ 2 -5 4.4 -96.6]

– The values have a pattern. For example: [10, 20, 30 ,…100] or [-10 -8 -6 -4 -

2 0]

– Or even, the total number of values is known! For example: 25 points evenly spaced from 0 to 100.

2.2. Patterns

• For patterns, there’s no great need of code!

The range operator

Numbers are separated by +1

2.2. Patterns, cont.

• Add an increment to increase by a different amount than +1

An additional value in the middle specifies the increment (aka step-size).

+3 +3 +3 +3 +3 +3 +3

+3 >32

2.2. Patterns, cont.

• Create a decreasing pattern by using a negative increment!

CAUTION: Now the beginning number must be > the end number.

Note: it works with fractional values.

-2.5 -2.5 -2.5 < 3

2.3. Specific number of data points

• Sometimes, the increment isn’t so important (or known) vs. HOW MANY points there are.

• A built-in function called linspace() spaces elements linearly in an array.– What does this mean?

• The distance between consecutive data points is a constant across the array.

>>doc linspace <enter>

linspace

Generate linearly spaced vectors

Syntax

y = linspace(a,b)y = linspace(a,b,n)

Description

The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points.y = linspace(a,b) generates a row vector y of 100 points linearly spaced between and including a and b.y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. For n < 2, linspace returns b.

2.3. linspace(), cont.

• MATLAB runs out of space to display

When MATLAB cannot display all the elements on one line, it simply indicates the column numbers for each line.

2.3. linspace(), cont.

• Transpose the return value of linspace() to create a column vector

2.3. linspace(), cont.

?????? %no third argument

Omit the third argument uses a default of _______ data points!

3. Creating Matrices

• Simply a combination of all operators introduced with vectors!– Square brackets [ ]– Spaces or commas , ,– Semi-colons ;– Apostrophes ‘

• Just keep in mind:only RECTANGULAR matrices

X

3.1. Matrices: hard-coding

• Use semi-colons to create new rows.

• Good or bad? Why?

2 by 3 3 by 2

3.2. Concatenating matrices

• Assume variable a from the previous slide. Use it as a reference to create a new variable:

“CONCATENATING”The act of “gluing”

vectors and matrices together

3.3 Using colons

• Combine ALL methods necessaryJUST KEEP THE ARRAY RECTANGULAR

Array Building

Array Building is the technique analogous to running totals – take an existing array and “augment it”, storing it back into the same variable:

my_array = [];value = input('An integer: ');my_array = [my_array, value];

Terminology

ConcatenationTo “glue together” two or more items to make a single item

AugmentationTo make an item bigger by appending / prepending / insertion

DiminutionTo make an item smaller by removal

24

II. Array Referencing

• Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions of the array or even individually.

• Because the values contained within the array may change when the program runs, the index (i.e. position) of the elements allows a mean of accessing them.

• MATLAB starts counting at 1.

? …

? …

3RD

25

II. Array Referencing

• How to refer to an element within a scalar? A vector? A matrix?

• A scalar has one single value – simply refer to the variable itself.age

• A vector has one dimension regardless whether it’s a row vector or a column vector. Use a single index to reference the values in a vector:ages(2)

• A matrix has two or more dimensions. Use an index for EACH dimension: FIRST: a row number, SECOND: a column number

pressures(3,56) (More dimensions? Use another number for each additional dimension!)

Array Referencing - Vectors

• Vectors use a single value. Each value is called an “index”:x = [5; -1; 4]; %original vectorsum = 0; %start sum at zerosum = sum + x(1); %add first elementsum = sum + x(2); %add second

elementsum = sum + x(3); %add third element

26

Index

This process of repeatedly adding numbers to a single variable is called a “running sum”

Array Referencing - Vectors

• Vectors use a single value. Each value is called an “index”:x = [5; -1; 4]; %original vectorsum = 0; %start sum at zerosum = sum + x(1); %add first elementsum = sum + x(2); %add second

elementsum = sum + x(3); %add third element

• Vectors have one dimension, so use a single index in parentheses to specify which element to use. Indexing starts at 1, and can go as high as how-many-elements-there-are.

Yes, it seems quite repetitive… wouldn’t a loop make it easier? Hang in there…

27

Array Referencing - Matrices

• Matrices are similar. To access the 6 in this matrix:

M = [1, 2, 3; 4, 5, 6; 7, 8, 9]

Use : M(2,3)

28

Row number always first!

Column number always second!

Array Referencing - Matrices

• Matrices are similar. To access the 6 in this matrix:

M = [1, 2, 3; 4, 5, 6; 7, 8, 9]

Use : M(2,3)

• It can be used directly:

x = 7 * M(2,3); %Result? _____

29

Row number always first!

Column number always second!

42

Array Referencing - Matrices

• Matrices are similar. To access the 6 in this matrix:

M = [1, 2, 3; 4, 5, 6; 7, 8, 9]

Use : M(2,3)

• It can be used directly:

x = 7 * M(2,3); %Result? _____

• The row and column positions specified in the parentheses are referred to as “indices” (plural of “index”): 2 is the “row index” and 3 is the “column index”.

30

Row number always first!

Column number always second!

42