Download presentation

Presentation is loading. Please wait.

1
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays Arrays chapter 2, pages 27-48 Scripts chapter 4, pages 77-81

2
Recall from Last Week Variables: placeholders for numerical data – equal sign is an assignment operator c = 7.5 c = c + 1 – naming restrictions (not pi, etc. ) – can be complex valued ( x = 3 + i 7 ) Basic math on numbers and variables: Precedence ( ) ^ * / + -

3
Uniformly 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!!

4
Arrays Fundamental data unit in Matlab – all variables are stored as arrays Data values organized into rows and columns – numeric or alphanumeric entries

5
Array 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

6
Building Arrays >>a = [1 2 3; 4 5 6; 7 8 9; 10 11 12] >>a = [1:3; 4:6; 7:9; 10:12]

7
Addressing and Sub-Arrays How to indicate a particular element within an array: – use parentheses after the array name – list desired row, comma, desired column – e.g. yield(2,4)

8
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 a vector, typically in brackets – e.g. yield([1 2],[3 4])

9
Addressing Errors

10
Things that do Work Single indexing of matrices counts down columns, starting at the top left

11
Some Useful Array Operators Transpose (single quote symbol ' ) – switches rows and columns

12
Useful Array Functions length(A) is the number of elements in the vector A [m n] = size(A), where A is a matrix with m rows and n columns ones(n) is an n x n matrix of ones zeros(n) is an n x n matrix of zeros

13
Character Arrays Rows of the array are strings of alphanumeric characters, one array entry per character Enter using a single quotation mark ( ' ) at each end

14
Assigning values with too large an index just grows the array

15
Scalars work for sub- array replacement – they just scale up to the right size

16
Replacing with a null matrix is the same as deleting – but it only works for entire rows or columns

17
Scripts – 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

18
Script (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

19
Sample Scripts

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google