slide2

advertisement
IT1005
Lab session on week 6
(2nd meeting)
Lab 2 - Recap
• By now, you must ensure that you are familiar
with Matlab environments:
– Matlab main window, M-File Editor, Command Window,
Workspace, Figure Window, Docking, etc.
– Matlab debugger, breakpoint, stepping functions, looking
at workspace during stepping, ‘go to cursor’, adjusting
program based on the error messages, etc.
• Remember that you can always just select Matlab
codes using ‘Select tool’ in Adobe Acrobat, then do
copy-paste to Matlab rather than typing it all again.
– For lab assignments, for trying codes in lecture notes, etc.
Symbols and Keyboard Locations
Symbol English Name
Keyboard Location
Matlab example
:
Colon
SHIFT+two keys on the left of [Enter]
A = 1:5
;
Semicolon
Two keys on the left of [Enter]
A = [1;2;3]
`
Apostrophe 1
One key on the left of key 1
NOT valid!
‘
Apostrophe 2
One key on the left of [Enter]
A = ‘hello’
[]
Square bracket
Above [Enter]
A = [1 2 3]
&
Ampersand
SHIFT+key 7
true && true
|
Bar
SHIFT+one key above [ENTER]
false || true
~
Tilde
SHIFT+one key one the left of key 1
~ false
The other keys should be quite straightforward…
http://en.wikipedia.org/wiki/Keyboard_layout#US
Review on Array/Vector/Matrix
• Creation
– mat = [], repmat, ones, zeros, eye, diag, rand, etc
• Accessing elements/indexing (index start from 1!)
– mat(indices), the usage of ‘:’, mat([indices1 … indicesn])
• Manipulation: add, edit, delete row/col, sort, find
– mat(:,x) = y
• Operation: +, -, *, /, .*, ./
– matA op matB, mat op scalar (scalar expansion)
• Multidimensional array: 3-D…
– We seldom go beyond 3D…
Quick Test
•
Simple test to about array:
1. Create an array of size 4*5 with all ‘1’ (then all ‘0’).
1.
Try repmat, manual creation, using [1;1;1;1]*[1 1 1 1 1] =)
2. Compute this: [1 2] .* [2 1] =
[3 4]
[2 1]
1.
Change .* to *, ./, /, +, or -
3. Create this array [1 2 3; 4 5 6; 7 8 9],
then perform these operations:
1.
2.
3.
Change the 2nd row to 100 90 80!
Change the 3rd col to 20 10 0!
Change the main diagonal to all zeros!
Relational and Logical Operations
• Matlab relational operations
– <, >, <=, >=, ==, ~=
– relational operations on array  the result is array
– floating-point imprecision  use eps
• Logical operators
– &, |, ~, &&, ||
– short circuit
Quick Test
•
Simple test to about relational/logical operations:
–
–
How to make these statements produce ans = 1
You can only supply ONE single operator
or MATLAB function to replace ‘…’
1.
2.
3.
4.
5.
6.
10+30*2-5/4 … 2*5-10
sin(pi) == 0?
… ( [10 20 30] / 10 ... [1 2 3] )
10 > 20 ... 5 … 2
10 > 5 … 5 > 2
… … 10 > 20 && 5 < 2 …
Lab 3 – Free and Easy Time
• Now, you are free to explore Matlab,
especially to find answers for Lab 3
• For those who have done Lab 3, try this extra challenges!
The solution is at the end of this session
– There is a land of size 3m*5m
– The height of each square meter on that land is known:
[1 2 3 4 5; 2 3 4 5 6; 7 7 7 7 7] above sea level (denoted in meter).
– A house of size 3*5 is going to be built there.
– Before building it, the land must be leveled!
– We want the land to have height 5m above sea level.
– Question: what is the minimum amount of soil (in cubic meter) that
must be added (or perhaps removed) in order to make this land flat?
– Hint: explore array operations, Matlab function ‘sum’, and ‘mesh’ or ‘surf’!
Lab 3 Marking + Midterm Test
• I will be away
– @ Jakarta, Indonesia during mid semester break
– 22 Feb night – 2 March
• Lab 3 marks will be available on 3 or 4 March.
– Will not be as fast as Lab 0/1 last week.
• If you have any questions, try IVLE forum first,
ask your class mates, or ask Colin.
– I will occasionally check my emails from home,
but will not be able to reply that fast!
– Or ask on Monday morning, before your Mid-term exam!
Remember This!
• E-mail answers to: stevenhalim at gmail.com
• With subject header:
– “IT1005-LabGroupID” “FullName”,
• e.g. IT1005-7A StevenHalim
– Mr Gmail will sort your emails for me =)
• Try to submit your answers ONCE
– Do not submit again to fix some minor mistakes!
• Shorter (but correct) answers ~ better marks =)
• Remember, I have ~100 emails/week !
Download