09b_CreatingArrays_forPrinting

Download Report

Transcript 09b_CreatingArrays_forPrinting

Creating Arrays
Creating scalars, vectors, matrices
Ex1 & 2. Dot Product & Cross Product
Ex3. Plotting Graphs
Ex4. Conversion Table
Ex5. Plotting functions
Finishing Ex4.
Ex6 and Ex7. Use of matrices in real world
1
1. Creating scalars

Assign a value to a variable (i.e. Hardcode)
pressure = 10;
temperature = 298;

%pascals
%kelvin
Store the result of an equation
pressure = density*R*temperature;

Save the return-value of the input() command
age = input(‘Enter your age: ’);
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 (addition only). For example:
[10, 20, 30 ,…100]
or
[-10 -8 -6 -4 -2 0]

Finally, the total amount of values is known. For example:
25 points evenly spaced from 0 to 100.
3
2.1. Pre-defined values
4
2.1. Pre-defined values, cont.
5
2.1. Pre-defined values, cont.
What else are semi-colons
used for?
6
2.1. Pre-defined values, cont.
They create rows AND
suppress
What elseoutput!
are semi-colons
used for?
7
2.1. Pre-defined values, cont.
The apostrophe allows to
transpose
vector.
Rows
They
createarows
AND
become columns.
Columns
suppress
What elseoutput!
are semi-colons
become rows.
used for?
8
2.1. Pre-defined values, cont.
The apostrophe allows to
transpose
vector.
Rows
They
createarows
AND
become columns.
Columns
suppress
output!
What dimension
will
speeds
have?
What else
are
semi-colons
become rows.
_______________________________
used for?
9
Ex1. Dot product

Remember the DOT product? (maybe/maybe not)
Credits to:
http://www.itee.uq.edu.au/~c
ogs2010/cmc/chapters/Hebbi
an/ten5.gif
The DOT product…

In Matlab
10
Ex2. Cross product

How about the CROSS product? (maybe/maybe not)
Source: Wikipedia
*
*
*
*
*
*
The CROSS product…
Source:
http://www.math.umn.edu/~nykamp/m
11
2374/readings/crossprodex/
Cross product, cont.

In Matlab
12
Ex3. Plotting graphs

In order to plot, Matlab needs data points:
x

y
-7
4
-2
-7
3
3
8
-1
y
x
Well…



x is an array of data points
y is another array of data points
…for the curious ones, to plot:
x = [-7 -2
y = [4 -7
plot(x,y)
3 8]
3 -1]
13
2.2. Patterns (addition only)
The range operator
Numbers are separated by +1
14
2.2. Patterns, cont.
The range operator
Numbers are separated by +1
An additional value in the middle
specifies the increment.
+3
+3
+3
+3
+3
+3
+3
+3
>32 
15
2.2. Patterns, cont.
The range operator
Numbers are separated by +1
An additional value in the middle
specifies the increment.
+3
+3
-2.5
Go reverse by using a negative increment! CAUTION: the
beginning number must be > the end number. Here 10>3. (This
also shows
it+3works
+3
+3
+3with decimals.)
+3
+3 >32 
-2.5
-2.5
<3
16
2.2. Patterns, cont.
The range operator
+3
+3
-2.5
Numbers are separated by +1
An additional
To use the apostrophe
andvalue in the middle
specifies
create a column
vector,the increment.
absolutely place brackets
first!
+3 +3
+3
+3
+3
+3 >32 
… else….
-2.5
-2.5
<3
17
2.2. Patterns, cont.
The range operator
+3
+3
-2.5
Numbers are separated by +1
An additional
To use the apostrophe
andvalue in the middle
specifies
the increment.
create
a column
vector,
Only
the scalar
-10 gets transposed: but a
absolutely
place brackets
scalar transposed
remains the same scalar!
first!
+3 +3
+3
+3
+3
+3 >32 
… else….
-2.5
-2.5
<3
18
Ex4. Conversion table
% create celsius data points
celsius = 0:10:100; %0 to 100 by +10 increment
% calculate Fahrenheit
fahrenheit = celsius * 9/5 + 32;
% show table
<code not shown>
19
2.3. Specific amount of data points

A built-in function called linspace() spaces elements
linearly in an array.

What does this mean?


The distance between each consecutive data point is equal.
There are two ways to use it, as Matlab ‘hints’ when the
command typed is unfinished:
Either provide 2 arguments, or
provide 3 arguments.
20
2.3. linspace(), cont.
The third argument indicates the
________________________ .
21
2.3. linspace(), cont.
The third argument indicates the
________________________ .
When Matlab cannot display all the
elements on one line, it simply
indicates the column-number per line.
22
2.3. linspace(), cont.
The third argument indicates the
________________________ .
When Matlab cannot display all the
elements on one line, it simply
indicates the column-number per line.
23
2.3. linspace(), cont.
?????? %no third argument
Omit the third argument uses a default of _______ data points!
24
Ex5. Plotting graphs

Suppose a function that relates each x to its y-coordinate
is known: y = f(x) = x2.  Plot y vs. x.

In this case, it is tedious work to hard-code each x and y
array. Are 4 data-points sufficient, like in example 3?
x
y
-10
100
-5
25
5
25
10
100
y
x
Does this represent f(x) = x2 ?
Yes Or No
25
Ex5. Plotting f(x) = x^2, cont.

Remember: which built-in function influences the number
of data-points in an array?____________________

In this case:
%array x of 20 data points
x = linspace(-10,10,20);
%calculate array of y’s.
y = x.^2;
%(The dot will be explained next time…)
%plot command
plot(x,y)
And the result is…
26
Ex5. Plotting f(x) = x^2, cont.
Does this represent
f(x) = x2 ?
Yes Or No
Yes, but it took 20
points!!
27
Ex5. Plotting f(x) = x^2, cont.

The use of linspace() in this example is crucial! Why
do all 20 data point need to be linearly spaced?

What would happen otherwise?
Still 20 points!!
.. but the first 19 are
before -5,
.. and the last one is 10.
Not f(x) = x2..
28
3. Creating Matrices

Simply a combination of all symbols introduced with
vectors!




Square brackets [ ]
Spaces or commas ,
Semi-colons ;
Apostrophes ’
,
29
3.1. Matrices: hard-coding
Use semi-colons to create new rows.
ONLY rectangular matrices:
The number of columns
MUST match for each row, and
vice-versa.
30
3.2. Reusing Previous matrices
Use semi-colons to create new
rows.
ONLY rectangular matrices:
Use
previous
matrices
to
The
number
of columns
actually
createfor
new
MUST match
each row, and
matrices.
vice-versa.
This example transposes
the matrix variable a.
31
3.3. Using Colons
Use semi-colons to create new
rows.
ONLY rectangular matrices:
You
cannumber
use previous
The
of columns
Combine any previous
matrices
to actually
create
MUST match
for each
row, and
methods, AS LONG AS
new
matrices.
vice-versa.
the matrix remains
rectangular.
This example transposes
the variable a.
32
3.4. “Concatenating”
Use semi-colons to create new
rows.
Finally, create
arrays
ONLY rectangular
matrices:
byprevious
combining
You
cannumber
use
The
of columns
You can
combine
any
variables!
matrices
toprevious
actually
create
MUST match
for each
row, and
previous methods, AS
new
matrices.
vice-versa.
LONGThis
AS the
matrix
is called
remains
rectangular.
CONCATENATING.
This example
transposes
the variable a.
33
3.5. Using the command window
Use semi-colons to create new
rows.
ONLY rectangular matrices:
You
cannumber
use previous
The
of columns
You can combine any
matrices
to actually
create
MUST match
for each
row, and
previous methods, AS
new
matrices.
vice-versa.
LONG AS the matrix
remains rectangular.
This example transposes
the variable a.
When the array
becomes too
big, the
numbers no
longer display.
34
Ex4. Conversion table, end!
% create celsius data points
celsius = 0:10:100; %0 to 100 by +10 increment
% calculate Fahrenheit
fahrenheit = celsius * 9/5 + 32;
% show table
[celsius’ fahrenheit’]
35
Ex6. Sling Thermometer
A method to read
relative-humidity.
36
Ex7. Images
Each row and
column have a pixel
value stored.
37
Wrapping Up

Know by heart each way to create a row/column vector.

Hard-code each data point






Shortcut when there is an addition pattern (colon)
Shortcut when a specific amount of data points are linearly
spaced (linspace())
Realize that creating matrices only requires combining
all of the above, while respecting one crucial rule:


Separate each data-point by comma or spaces for row vector
Separate each data-point by semicolon for a column vector
A matrix must remain rectangular at all times (i.e. no holes within
the matrix)
What does the apostrophe do?
Restate some examples of vector operations and matrix 38
operations.