Transcript ppt

CS100A, Fall 1998
Lecture 19, Thursday Nov 05
Matlab
Concepts:
• Matlab arrays
• Matlab subscripting
• Matlab plotting
CS100A, Fall 1998, Lecture 19
1
Arrays (review)
• All data in Matlab is actually an array (or a
vector) — a 1- or 2-dimensional table of
numbers.
• A single value, called a scalar, is simply an
array of size 1.
• To construct a 1-D array, list its elements
surrounded by square brackets.
y = [4 -5 10 0 5.2]
• Individual elements are accessed using a
parenthesized subscript.
x(3)
• The first element of array x is x(1).
• The number of elements in array x is given
by the built-in function
length(x)
CS100A, Fall 1998, Lecture 19
2
Creating Arrays (review)
• An array of evenly-spaced values can be
generated by
linspace(minVal, maxVal, nVals)
• Two arrays can be combined with a comma
and brackets:
x = [1 2 3];
y = [4 5 6];
[x, y]
(is [1 2 3 4 5 6])
z = [x, [x, y]];
CS100A, Fall 1998, Lecture 19
3
Creating Arrays (colon)
• The colon can be used to generate a
sequence of values. Forms:
lowValue : highValue
lowValue : step : highValue
Examples:
1 : 10
1 : 2 : 10
1 : 0.5 : 10
10 : –1 : 1
0 : 0.01 : 0.5
• A sequence of values is an array value.
a = 0 : 2 : 16
b = [1 : 6] / 3
• A sequence of integers can also be used to
select a segment of an array.
a(3:6)
CS100A, Fall 1998, Lecture 19
4
Array Functions
• There are many functions to compute facts
about arrays.
min(x), max(x), mean(x), ...
Array Operations
• Basic operations on arrays are performed
element-by-element. Example: function
applications:
x = [4.2 7.89 2.4 -42.1 ]
floor(x)
• An operation may involve an array and a
scalar. The operation is performed on each
element of the array and the result is an
array of these values.
x/2
CS100A, Fall 1998, Lecture 19
5
Array Operations (cont.)
• Operations may combine two arrays if they
have exactly the same length.
x = [1 3 5 7]
y = [10 20 30 40]
x+y
y-x
• The operations + and – work as expected.
• For element-by-element multiplication,
division, and exponentiation, use .* , ./ ,
and .^ .
(Explanation: The usual operators * , / ,
and ^ perform matrix [Linear algebra]
operations between arrays. We will not
cover that in CS100.)
CS100A, Fall 1998, Lecture 19
6
Multiple Subscripts
• In general, an array of integers can be used
as a subscript. The result is an array
consisting of the elements selected by the
subscripts in the order given.
a = 10 * [0:9]
a([3 4 5])
a(3:5)
a([5 4 3])
a([1 1 7 4 6])
a(10:-1:1)
CS100A, Fall 1998, Lecture 19
7
Logical Operations & Arrays —
0/1 Arrays
• Logical operations yield 0 (false) or 1
(true). When performed on arrays, an array
of 0’s and 1’s is the result.
a = [5 8 6 12 9]
b = [2 3 6 7 10]
a>b
a ~= b
a>5
rem(a, 3)
rem(a, 3) == 0
• The functions any and all yield 1 if,
respectively, any or all of the elements in
their array argument are non-zero.
CS100A, Fall 1998, Lecture 19
8
Selecting Elements with 0/1 Arrays
• If a vector of 0’s and 1’s is used as a
subscript of an array of the same length, the
result is a new array containing only those
elements of the old array with a 1 subscript.
• This is especially useful when the result of a
logical expression is used as a subscript to
select array elements based on some
condition.
a = [12 7 21 3 8 14 0 6]
a([0 1 1 0 0 0 1 0])
b = a - 10
b>0
b( b > 0 )
a( rem(a,3) ~= 0 )
CS100A, Fall 1998, Lecture 19
9
Managing the Work Session
clc
clear
help name
Clears the Command window
Removes variables from memory
Searches online help for the
topic name
lookfor name Searches the help entries for the
specified keyword name
quit
Stops Matlab
who
Lists the variables currently in
memory
whos
Lists the current variables and
sizes, and indicates whether they
have imaginary parts
CS100A, Fall 1998, Lecture 19
10
Basic Plotting
• If x and y are two arrays with the same
number of elements, plot(x,y) draws a plot
of x (horizontal) vs y (vertical)
x = linspace(0, 4*pi, 250);
y = sin(x);
plot(x,y)
• Normally the graph is scaled so the full
range of x and y values fill the plot. To
have equal spacing on the axes, enter
axis(‘equal’)
after the plot has been drawn (using straight
quote marks).
• You can label the axes and title the graph
after it has been drawn:
xlabel(‘x axis label’)
ylabel(‘y axis label’)
title(‘A Fabulous Graph’)
CS100A, Fall 1998, Lecture 19
11
Plot Options
• The plot command has an optional third
argument that can be used to specify the
line color and style. Examples:
v = -10:0.5:10;
fv = 3*pi*sin(v).^2 - v;
plot(v, fv, ‘g’); % green line
plot(v, fv, ‘b:’); % blue dotted line
plot(v, fv, ‘r+’); % red crosses
plot(v, fv, ‘c--’); % cyan dashed line
• Use help plot to find other possibilities
CS100A, Fall 1998, Lecture 19
12
Multiple Plots
• Normally each new plot is drawn in a blank
window, replacing whatever is there. Use
hold on to retain the previous plot so you
can draw a new one over it. Use hold off to
release the previous plot so the next one
will appear in a blank window. Example:
x = linspace(0, 6*pi, 1000);
y = sin(x);
z = cos(x);
plot(x, y, ‘r’);
hold on
plot(x, z, ‘g’);
CS100A, Fall 1998, Lecture 19
13