Title: E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS
1E77 INTRODUCTION TO COMPUTER PROGRAMMING FOR
SCIENTISTS AND ENGINEERS
- Lecture Outline
- 1. Introduction to arrays
- 2. One-dimensional arrays
- 3. Character strings
2Introduction to arrays
- A simple array is an ordered collection of real
numbers. - MATLAB treats arrays very efficiently
- Input/output, indexing and addressing
- Arithmetic operations
- Other manipulations (e.g., sizing, reshaping,
etc.) -
- Arrays are the primary building blocks in MATLAB.
3Introduction to arrays
- Examples
-
-
(1 row, 1 column) -
(1 row,4 columns) -
-
-
(3 rows, 1 column)
4Introduction to arrays
- Examples 2-dimensional arrays
-
(2 rows, 3 columns) - (3 rows, 3 columns)
5One-dimensional arrays
- Those that have one row or one column.
- Construction
- Manual
- Incremental
- linspace
- transpose
- zeros
- ones
- rand/randn
6One-dimensional arrays manual construction
- Row vectors
- Syntax element1 , element2 ,
gtgtr 3,7,9 r 3 7 9
7One-dimensional arrays manual construction
- Row vectors
- Syntax element1 element2
gtgtr 3 7 9 r 3 7 9
8One-dimensional arrays manual construction
- Column vectors
- Syntax element1
element2
gtgtc 379 c 3 7 9
starts the array
separates rows of an array
ends the array
9Row vectors incremental construction
gtgt r 3 2 10 r 3 5 7 9
Syntax first element increment limit
10Row vectors incremental construction
gtgt r 3 2 10 r 3 5 7 9
11Examples incremental construction
first element increment limit
- gtgt A 1110
- A
- 1 2 3 4 5 6 7 8
9 10
- gtgt B 110
- B
- 1 2 3 4 5 6 7 8
9 10
(first element limit)
12Examples incremental construction
first element negative increment limit
- gtgt C 10 -2 -5
- C
- 10 8 6 4 2 0 -2 -4
13Linspace command
- also creates a linearly spaced row vector
- number of elements are specified instead of
increment - Syntax linspace(x1,x2,n)
- x1 lower limit
- x2 upper limit
- n - number of evenly-spaced elements
gtgt A linspace(3,9,4) A 3 5 7
9
14Linspace command
- also creates a linearly spaced row vector
- number of elements are specified instead of
increment - Syntax linspace(x1,x2,n)
- x1 lower limit
- x2 upper limit
- n - number of evenly-spaced elements
gtgt A linspace(3,9,5) A 3 4.5 6
7.5 9
15The transpose operator
'
- The transpose operator converts
- (row vector) (column vector)
- (column vector) (row vector)
gtgtr 3,7,9 r 3 7 9
gtgt c r' c 3 7 9
16zeros
- Syntax zeros(n,m)
- Create an array of zeros that has
- n rows
- m - columns
gtgt r zeros(1,3) r 0 0 0
gtgt c zeros(3,1) c 0 0
0
17ones
- Syntax ones(n,m)
- Create an array of ones that has
- n rows
- m - columns
gtgt r ones(1,3) r 1 1 1
gtgt c ones(3,1) c 1 1
1
18rand
- Syntax rand(n,m)
- Create an array of random numbers
- n rows
- m - columns
gtgt r rand(1,3) r 0.9501 0.2311
0.6068
19randn
- Syntax randn(n,m)
- Create an array of Gaussian random numbers
- n rows
- m - columns
gtgt r randn(1,3) r -0.4326 -1.6656
0.1253
20Length, and Absolute Value of a Vector
- length
- gives the number of elements in the vector.
- abs
- gives a vector whose elements are the absolute
values of the original vector - Commands apply to both row and column vectors
- Neither command gives the magnitude of the vector
21Length, and Absolute Value of a Vector
gtgt r -3 2 1 5 r -3 2 1 5 gtgt
length(r) ans 4 gtgt abs(r) ans 3
2 1 5
22Accessing single elements of a vector
- If A is a vector (i.e., a row or column vector),
then - A(1) is its first element,
- A(2) is its second element,
- A(end) is its last element
- Example
gtgt r -3 2 1 5 gtgt r(3) ans 1
23Accessing single elements of a vector
- This syntax can be used to assign an entry of A
- Syntax VariableName(Index) Expression
- Example
gtgt r -3 2 1 5 gtgt r(end) 2 r -3
2 1 2
24Accessing multiple elements of a vector
- If A is a vector (i.e., a row or column vector),
then - A(i,j,k) is a vector with i,j,k elements of A
- Example
gtgt r -3 2 1 5 gtgt r(2,3) ans 2 1
25Assigning multiple elements of a vector
- If A is a vector, then we can assign multiple
elements - A(i,j,k,) expression
- (must have consistent dimensions)
- Example
gtgt r -3 2 1 5 gtgt r(2,3) 1 2 ans
-3 1 2 5
26Unary Numeric Operations on Arrays
- Unary operations involve one input argument.
Examples - Negation, (using the - sign), abs
- Trig functions, sin, cos, tan, asin, acos, atan,
- General rounding functions, floor, ceil, fix,
round - Exponential and logs, exp, log, log10, sqrt
- Complex, abs, angle, real, imag
27Unary Operation Examples
- Trigonometric sin
- (arguments are assumed to be in radians)
gtgt r pi -1 gtgt sin(r) ans 0.0
-0.8415
28Binary (two argument) operations on Arrays
- Addition (and subtraction)
- If A and B are arrays of the same size,
- A B is the array A(i) B(i)
- Example
gtgt A B ans -1 3 gtgt A - B ans -5
1
gtgt A -3 2 gtgt B 2 1
29Binary (two argument) operations on Arrays
- Addition (and subtraction)
- If A is an array, and b is a scalar
- A b b A is the same size as A
- A(i) b
- Example
gtgt A b ans -1 4 gtgt A - b ans -5
0
gtgt A -3 2 gtgt b 2
30Binary (two argument) operations on Arrays
- Scalar-Array Multiplication
- If A is an array, and b is a scalar
- A b b A the same size as A
- A(i) b
- Example
gtgt A b ans -6 4 gtgt b A ans -6
4
gtgt A -3 2 gtgt b 2
31Binary (two argument) operations on Arrays
- Array Element-by-Element Multiplication .
- If A and B are arrays of the same size,
- A . B B . A is the array
- A(i) B(i)
- Example
gtgt A . B ans -6 2 gtgt B . B ans
4 1
gtgt A -3 2 gtgt B 2 1
32Binary (two argument) operations on Arrays
- Matrix Multiplication ()
- If A and B are arrays of consistent dimensions,
- A B the matrix multiplication of the two
arrays (more later) - Example
gtgt r c ans -4 gtgt c r ans -6
3 4 2
gtgt r -3 2 gtgt c 2 1
33Binary (two argument) operations on Arrays
- Matrix Multiplication ()
- If A and B are arrays of consistent dimensions,
- A B the matrix multiplication of the two
arrays (more later) - Example
gtgt r1 r2 ??? Error using gt Inner matrix
dimensions must agree.
gtgt r1 -3 2 gtgt r2 2 1
34Computing the magnitude of a vector
- Example
- How do we compute the magnitude of c ?
gtgt c -3 2 1 5 gtgt sqrt(c c) ans
6.2450
35Character strings
- In addition to numbers, many computer programs
also - need to deal with text data, e.g.,
- Names and addresses
- Input and output prompts
- Labels
- Text files
- Character strings in MATLAB are special arrays
displayed - in text format, but stored and manipulated in
numerical - format.
36Strings construction
- Use the single quote to create basic character
arrays
gtgt s 'Roberto is the teacher' s Roberto is
the teacher
37Strings manipulation
- Strings are special kinds of vectors
- Each character in the string is an element of the
array - Matlab converts characters using ASCII code
- E.g. b is the ASCII code 98
gtgt s 'Roberto is the teacher' gtgt
length(s) ans 22
gtgt s(end) ans r
38Strings manipulation
gtgt s 'Roberto is the teacher' gtgt s(1 4
6) ans Ret
gtgt s(end-11) ans rehcaet eht si otreboR
39Strings to double conversion
gtgt s 'Roberto '
gtgt v double(s) v 82 111 98 101 114
116 111 32
gtgt char(v) ans Roberto
40Finding a string with findstr
- Syntax k findstr(str1,str2)
- finds index of first occurrence of str1 in str2
gtgt s 'Roberto is the teacher' gtgt
findstr(the,s) ans 12
41Summary
- What did we learn today?
- Arrays are important data structures and can be
- treated with great efficiency in MATLAB.
- Many different MATLAB built-in functions can be
- employed to input, manipulate and output arrays.
- Strings are numerical arrays in disguise and are
- employed to treat text data.
- Many different MATLAB built-in functions can be
- employed to input, manipulate and output strings
and - string arrays.