Title: EGR 106 Week 2 Arrays
1EGR 106 Week 2 Arrays Scripts
- Arrays
- Brief review of some Week 1 ideas
- Construction, addressing, etc
- Script and the editor
- Textbook chapter 2, pages 27-48, chapter 4.1-4.3,
pages 77-81
2Review from Last Week
- Variables placeholders for numerical data
- equal sign is an assignment operator
- naming restrictions (not pi, etc. )
- can be complex valued ( x 3 7 i )
- Basic math on numbers and variables
- /
- Array Fundamental data unit in Matlab
- all variables are stored as arrays
3- Data values organized into rows and columns
- numeric or alphanumeric entries
- each element has position and value
- Size or dimension number of rows and columns of
the array (rows by columns) - Scalar, row vector, column vector, matrix
3 by 4 array
4Array Construction
- Direct specification
- Name followed by an equal sign ( ),
just like variables - List values within a pair of brackets ( )
- Enter data one row at a time
- left to right, top to bottom order
- space or comma between the values
- rows separated by semicolons or the enter key
5- For example, for
-
- use
- b 4,5,3,9 10,4,66,20 18,-3,2,0
- or
- b 4, 5, 3, 9
- 10, 4, 66, 20
- 18, -3, 2, 0
enter
enter
enter
enter
6- Can use math operators as well as numerics as the
entries - Note the common format of all entries in the
response (exp(1) e 2.71828, log10(100) 2,
2-12 0.00024414) MATLAB scales the exponent
to the largest entry
7- This scaling is sometimes deceptive
Not really zero
Really zero
8- Special predefined arrays
- A matrix of all zeros
- zeros(R,C) zeros(N)
- A matrix of all ones
- ones(R,C) ones(N)
- A matrix of zeros with ones on the diagonal
- eye(R,C) eye(N)
- A matrix of random numbers (within 0 1 )
- rand(R,C) rand(N)
Square versions
9random on 0, 1
10- Concatenation gluing arrays together
- if a 1 2 3 b 4
5 6 - Attaching left to right use a comma
-
- a, b
- Attaching top to bottom use a semicolon
-
- a b
comma
semicolon
11- Note that sizes must match
-
- if a 1 2 3
- then
- a, b ?? a b ??
-
- Size needs for concatenation
- of rows the same for side by side (comma)
- of columns the same for top to bottom
(semicolon)
12- Evenly spaced vectors colon operator
- first increment maximum
- yields a row vector of equally spaced values
- examples
- 0 2 10 0 2 4 6 8 10
- 1 5 1 2 3 4 5
- 7 -2 -3 7 5 3 1 -1 -3
- 1 2 8 1 3 5 7
- default for increment is 1
Note does not hit 8!!
13- Linspace like the colon operator, but
definitely gets the last number on the list - linspace ( start, last, number of values)
-
- examples
- linspace(0,10,6) 0 2 4 6 8 10
- linspace(0,1,4) 0 0.333 0.667 1
- default for number is 100
- linspace(0,10) 0 0.101 0.202 10
Note increment for 100 points, 99 intervals
14Addressing and Sub-Arrays
- We indicate a particular element within an array
by its row/column position - use parentheses after the array name
- e.g.
yield(2,4) -
15- To read a value from an array
- Addressing on the right hand side of
16- To change a value in an array
- Addressing on the left hand side of
17- How about more than one entry?
- can specify a rectangular sub-array
- again, use parenthesis after the array name
- list desired rows, comma, desired columns
- as separate vectors, typically in brackets
- e.g.
-
yield(1 2,3 4) -
18- Example
-
- sample(3,2)
- sample (1,24)
- sample (3,4,3,2,1)
- sample (1,3,2,4)
19- Used to read a sub-array ( rhs of )
Note scalar row choice does not need brackets!
20- Used to change a sub-array (lhs of )
Note array on the right needs to be of the
correct size !!!
21- Additional notes
- end specifies the last row or column
- a colon ( ) specifies all of a row or column
-
- yield(2,)
- yield(end,1)
- use a single index for row and column vectors
- bob 9 7 5 7 2
bob(4)
22 23Wrong number of elements for the assignment
Wrong dimensions for assignment
24- Other useful methods that do work
Single indexing of matrices counts down columns,
starting at the top left
25Assigning values with too large an index just
grows the array
26Scalars work for sub-array replacement they
just scale up to the right size
27Replacing with a null matrix is the same as
deleting but it only works for entire rows or
columns
28- Rules of the road
- Symbols to use
- brackets to glue elements together to make an
array (left to right or top to bottom) - comma (or space) and semicolon (or enter) for
separating row/column elements - parentheses after the array name for addressing
- Be careful to match array sizes
- Rows first, then columns in addressing
-
29Some Useful Array Operators
- Transpose (single quote symbol ' )
- switches rows and columns
-
30- Size the number of rows and columns
- Length the larger of these two
-
Array answer
Scalar answer
31- Diag matrix vector operator for diagonal
elements -
32- Reshape
- resize fixed set of elements
-
Note the set of elements is exactly the
same Note column-wise approach to re-ordering the
values
33Character Arrays
- Rows of the array are strings of alphanumeric
characters, one array entry per character - Enter using a single quotation mark ( ' ) at each
end
34- For multi-row alphanumeric arrays, each row must
have the same number of characters - name 'Marty' 'James' 'Bob '
- Use 2 quotation marks in a row to get 1
- 'Mary''s' Mary's (a 1 by 6
array) - Also, there are some built-in arrays
- y date y 05-Jan-2004
- (a 1 by 11 array)
Note need 2 spaces
35Scripts Simple Programs
- So far, commands have been typed in the command
window - Executed by pressing enter
- Edited using the arrow keys or the history
window
36Script (m-file) Concept
- A file containing Matlab commands
- Can be re-executed
- Is easily changed/modified or e-mailed to someone
- Commands are executed one by one sequentially
- File is executed by typing its name (without .m)
- Results appear in the command window (or use )
- Can be created using any text editor
- .m extension
- Listed in Current Directory window
37(No Transcript)
38- Matlabs Built-in, Color Editor
- Can create a new file
- or open an existing
- M file (icons or click
- on file name)
- Color used to aid in
- file creation
- (command types,
- typos, etc.)
39- typical Windows menu
- line numbers
- run button or F5
- debug capability
- comment lines
- note use of semicolons
- note use of colors
40- Where Matlab looks for things
- As a variable in the current workspace
- As a built-in function
- As script in the current directory
- can redirect using the buttons marked
41- Down the search path of directories
- Conclusion use unique names
42- How scripts can get data
- From arrays in the current workspace
- From arrays defined in the script
- Using the input command
- Numeric
- x input(' how many? ')
- String
- x input(' name? ', 's')
43- How scripts can show data
- Command of the array name
- Using the display command
- Existing array (a single array only if
necessary, use !!) - disp(x) or disp(x y)
- Text
- disp(' The task is done ')
44- Example
- Note that disp shortens the resulting output by
dropping the array name and removing blank lines
45Sample Scripts
46(No Transcript)
47(No Transcript)
48Sound Arrays (not in the book!)
- Matlab can interface to microphones and speakers
through the computers sound card (sampled and
digitized) .e.g. maple
49- Matlab represents sounds using arrays
- (column vectors)
- Tools needed
- Microphone
- Speakers
- and
- 2 Matlab scripts
- init_sound.m
- sound_in.m
Headsets available in ECC
Files available on the egr106 website save them
in your work directory
50- How to use them
- Connect headset (is it muted?)
- Type init_sound at the command prompt (only
needed once) - Type sound_in at the prompt to record 1 second of
sound (waits for your input) - generates array named data
- Type sound(data,8000) at the prompt to play array
data - Can plot data, manipulate data before
replaying, etc.