Variables and Arrays - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Variables and Arrays

Description:

Size of an array is specified by the number of rows and the number of columns in ... Real and imaginary numbers can be in the range 10-308 to 10308 with 15 to 16 ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 25
Provided by: GOP77
Category:
Tags: an | arrays | imaginary | in | variables

less

Transcript and Presenter's Notes

Title: Variables and Arrays


1
Chapter 2
  • Variables and Arrays

2
  • http//web.njit.edu/vge2
  • website updated.

3
Array
  • It is the fundamental unit of data in any MATLAB
    program.
  • An array is a collection of data values organized
    into rows and columns, and known by a single name.

4
Arrays
  • Individual data values within an array are
    accessed by including the name of the array
    followed by subscripts in parentheses that
    identify the row and column of the particular
    value.
  • Single dimensional arrays ? vectors
  • Two or more dimensions arrays ? matrices
  • Size of an array is specified by the number of
    rows and the number of columns in the array, with
    the number of rows mentioned first.
  • Total number of elements in the array will be the
    product of the number of rows and number of
    columns
  • Store the elements by column-wise in memory.

5
Arrays
6
Arrays
7
Arrays example
8
variable
  • It is a region of memory containing an array,
    which is known by a user-specified name i.e. a,
    b, c in the previous example.
  • Variable names are case-sensitive.
  • MATLAB variable names must begin with a letter,
    followed by any combination of letters, numbers
    and the underscore( _ ) character.
  • Names with more than 63 characters are truncated
    to 63.

9
Variable types
  • Double - principle data type in MATLAB.
  • Holds real, imaginary, or complex values.
  • Real and imaginary numbers can be in the
    range 10-308 to 10308 with 15 to 16 significant
    decimal digits of accuracy.
  • e.g. Real var 10.5, imaginary var
    4i,
  • complex number var 1010i
  • Char - holds character strings
  • e.g. varThis is a character string
  • (note place the characters in between
    single quotes)

10
Creating arrays
11
Creating arrays cont..
  • empty matrix.
  • Use comma (,) or space to separate elements in
    the same row.
  • Use semicolon() to move to the next row.
  • Note All rows must have the same number of
    columns.
  • Arrays can be initialized using algebraic
    operations and all of or portions of previously
    defined arrays.
  • e.g. a0 17 ? a0 8
  • ba(2) 7 a ? b8 7 0 8

12
Creating arrays cont..
  • Not all of the elements in an array must be
    defined when it is created.
  • eg c(2,3)5 -gt c 0 0 0 d1,2
    d(4)4 ?d1 2 0 4
  • 0 0 5 ,

13
Short cut expressions
  • Colon () operator
  • Short cut notation to create arrays
  • General form first increment last
  • eg x1210 ? x1 3 5 7 9
  • Transpose () operator
  • swaps the row and columns of any array that it is
    applied to.
  • Useful to initialize column vectors and more
    complex matrices.
  • eg f14 ? f 1 , g14 hg g ? h
    1 1
  • 2
    2 2
  • 3
    3 3
  • 4
    4 4

14
Initializing arrays with built-in functions
15
Built-in functions cont
  • eg azeros(2)
  • bzeros(2,3)
  • c1 23 4
  • dzeros(size(c))

16
Initializing variables with keyboard input
  • input()
  • eg in1 input(Enter data )
  • Enter data 1.23
  • ? stores the value 1.23 into in1
  • in2 input(Enter data ,s)
  • Enter data 1.23
  • ? stores the character string 1.23 into in2

17
subarrays
  • eg

18
end function
  • eg arr31 2 3 4 5 6 7 8
  • arr3(5end)?5 6 7 8 arr3(end)?5
  • Value returned by end is always the highest value
    of a given subscript.

19
Subarrays cont..
20
Subarrays cont..
  • Assigning scalar to an array
  • arr41 2 3 4 5 6 7 8 9 10 11 12
  • arr4(12,12)1
  • ?arr4 1 1 3 4
  • 1 1 7 8
  • 9 10 11 12

21
Predefined special values
22
Exercise
23
Exercise
24
Exercise
Write a Comment
User Comments (0)
About PowerShow.com