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)
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
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
pressure = 10; %pascals
temperature = 298; %kelvin
pressure = density*R*temperature;
age = input(‘Enter your age: ’);
2
[ 2 5 4.4 96.6]
3
[ 2 5 4.4 96.6]
[10, 20, 30 ,…100] or [10 8 6 4 2 0]
4
[ 2 5 4.4 96.6]
[10, 20, 30 ,…100] or [10 8 6 4 2 0]
25 points evenly spaced from 0 to 100.
5
They create rows AND suppress output!
What else are semicolons used for?
10
The apostrophe allows to transpose a vector. Rows become columns. Columns become rows.
They create rows AND suppress output!
What else are semicolons used for?
11
The apostrophe allows to transpose a vector. Rows become columns. Columns become rows.
They create rows AND suppress output!
What dimension will speeds have? _______________________________
What else are semicolons used for?
12
Credits to: http://www.itee.uq.edu.au/~cogs2010/cmc/chapters/Hebbian/ten5.gif
The DOT product…
Credits to: http://www.itee.uq.edu.au/~cogs2010/cmc/chapters/Hebbian/ten5.gif
The DOT product…
*
*
*
*
*
Ex2. Cross productSource: Wikipedia
The CROSS product…
Source: http://www.math.umn.edu/~nykamp/m2374/readings/crossprodex/
x y
7
2
3
8
4
7
3
1
x
Ex3. Plotting graphsMatlab connects the dots!
x y
7
2
3
8
4
7
3
1
x
Ex3. Plotting graphsThe 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
22
The range operator
Numbers are separated by +1
An additional value in the middle specifies the increment.
Go reverse by using a negative increment! CAUTION: the beginning number must be > the end number. Here 10>3. (This also shows it works with decimals.)
+3 +3 +3 +3 +3 +3 +3
+3 >32
2.5 2.5 2.5 < 3
23
The range operator
Numbers are separated by +1
An additional value in the middle specifies the increment.
To use the apostrophe and create a column vector, absolutely place brackets first!
… else….
+3 +3 +3 +3 +3 +3 +3
+3 >32
2.5 2.5 2.5 < 3
24
The range operator
Numbers are separated by +1
An additional value in the middle specifies the increment.
To use the apostrophe and create a column vector, absolutely place brackets first!
… else….
Only the scalar 10 gets transposed: but a scalar transposed remains the same scalar!
+3 +3 +3 +3 +3 +3 +3
+3 >32
2.5 2.5 2.5 < 3
25
% create celsius data points
celsius = 0:10:100; %0 to 100 by +10 increment
% calculate Fahrenheit
fahrenheit = celsius * 9/5 + 32;
% show table
2.3. Specific amount of data points A builtin 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.
27
2.3. linspace(), cont.The third argument indicates the ________________________ .
28
2.3. linspace(), cont.The third argument indicates the ________________________ .
When Matlab cannot display all the elements on one line, it simply indicates the columnnumber per line.
29
2.3. linspace(), cont.The third argument indicates the ________________________ .
When Matlab cannot display all the elements on one line, it simply indicates the columnnumber per line.
30
2.3. linspace(), cont.?????? %no third argument
Omitthe third argument uses a default of _______ data points!
Ex5. Plotting graphs Suppose a function that relates each x to its ycoordinate is known: y = f(x) = x2. Plot y vs. x.
x y10
5
5
10
100
25
25
100
Ex5. Plotting graphs Suppose a function that relates each x to its ycoordinate is known: y = f(x) = x2. Plot y vs. x.
 In this case, it is tedious work to hardcode each x and y array. Are 4 datapoints sufficient, like in example 3?
y
x
Ex5. Plotting f(x) = x^2, cont. Remember: which builtin function influences the number of datapoints in an array?____________________
 In this case:
%array x of 20 data points
%calculate array of y’s.
%plot command
And the result is…
Ex5. Plotting f(x) = x^2, cont. Remember: which builtin function influences the number of datapoints 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…
Ex5. Plotting f(x) = x^2, cont.Does this represent f(x) = x2 ?
Yes Or No
Yes, but it took 20 points!!
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..
3. Creating Matrices Simply a combinationof all symbols introduced with vectors!
 Square brackets [ ]
 Spaces or commas , ,
 Semicolons ;
 Apostrophes ’
38
3.1. Matrices: hardcoding Use semicolons to create new rows.
 ONLY rectangular matrices:
 The number of columns MUST match for each row, and viceversa.
39
3.2. Reusing Previous matrices Use semicolons to create new rows.
 ONLY rectangular matrices:
 The number of columns MUST match for each row, and viceversa.
Use previousmatrices to actually create new matrices.
This example transposes the matrix variable a.
40
3.3. Using Colons Use semicolons to create new rows.
 ONLY rectangular matrices:
 The number of columns MUST match for each row, and viceversa.
You can use previousmatrices to actually create new matrices.
This example transposes the variable a.
Combine any previous methods, AS LONG AS the matrix remains rectangular.
41
3.4. “Concatenating” Use semicolons to create new rows.
 ONLY rectangular matrices:
 The number of columns MUST match for each row, and viceversa.
Finally, create arrays by combining previous variables!
This is called
CONCATENATING.
You can use previousmatrices to actually create new matrices.
This example transposes the variable a.
You can combine any previous methods, AS LONG AS the matrix remains rectangular.
42
3.5. Using the command window Use semicolons to create new rows.
 ONLY rectangular matrices:
 The number of columns MUST match for each row, and viceversa.
When the array becomes too big, the numbers no longer display.
You can use previousmatrices to actually create new matrices.
This example transposes the variable a.
You can combine any previous methods, AS LONG AS the matrix remains rectangular.
43
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’]
Ex6. Sling ThermometerA method to read relativehumidity.
Ex7. ImagesEach row and column have a pixel value stored.
Wrapping Up Know by heart each way to create a row/column vector.
 Hardcode each data point
 Separate each datapoint by comma or spaces for row vector
 Separate each datapoint by semicolon for a column vector
 Shortcut when there is an addition pattern (colon)
 Shortcut when a specific amount of data points are linearly spaced (linspace())
Wrapping Up Know by heart each way to create a row/column vector.
 Hardcode each data point
 Separate each datapoint by comma or spaces for row vector
 Separate each datapoint by semicolon for a column vector
 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:
 A matrix must remain rectangular at all times (i.e. no holes within the matrix)
Wrapping Up Know by heart each way to create a row/column vector.
 Hardcode each data point
 Separate each datapoint by comma or spaces for row vector
 Separate each datapoint by semicolon for a column vector
 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:
 A matrix must remain rectangular at all times (i.e. no holes within the matrix)
 What does the apostrophe do?
Wrapping Up Know by heart each way to create a row/column vector.
 Hardcode each data point
 Separate each datapoint by comma or spaces for row vector
 Separate each datapoint by semicolon for a column vector
 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:
 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 operations.























Download Presentation
Connecting to Server..