CS100A, Fall 1998 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS100A, Fall 1998

Description:

after the plot has been drawn (using straight quote marks) ... plot(v, fv, c--'); % cyan dashed line. Use help plot to find other possibilities ... – PowerPoint PPT presentation

Number of Views:8
Avg rating:3.0/5.0
Slides: 14
Provided by: HalPe1
Category:
Tags: cs100a | fall | line | quote

less

Transcript and Presenter's Notes

Title: CS100A, Fall 1998


1
CS100A, Fall 1998
  • Lecture 19, Thursday Nov 05
  • Matlab
  • Concepts
  • Matlab arrays
  • Matlab subscripting
  • Matlab plotting

2
  • 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)

3
  • 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

4
  • 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(36)

5
  • 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

6
  • 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.)

7
  • 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 09
  • a(3 4 5)
  • a(35)
  • a(5 4 3)
  • a(1 1 7 4 6)
  • a(10-11)

8
  • Logical Operations Arrays
  • 0/1 Arrays
  • Logical operations yield 0 (false) or 1 (true).
    When performed on arrays, an array of 0s and 1s
    is the result.
  • a 5 8 6 12 9
  • b 2 3 6 7 10
  • a gt b
  • a b
  • a gt 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.

9
  • Selecting Elements with 0/1 Arrays
  • If a vector of 0s and 1s 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 gt 0
  • b( b gt 0 )
  • a( rem(a,3) 0 )

10
  • Managing the Work Session
  • clc Clears the Command window
  • clear Removes variables from memory
  • help name 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

11
  • 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, 4pi, 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)

12
  • Plot Options
  • The plot command has an optional third argument
    that can be used to specify the line color and
    style. Examples
  • v -100.510
  • fv 3pisin(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

13
  • 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, 6pi, 1000)
  • y sin(x)
  • z cos(x)
  • plot(x, y, r)
  • hold on
  • plot(x, z, g)
Write a Comment
User Comments (0)
About PowerShow.com