Docsity

MATLAB Basics: Variables, Arrays, and Operations, Slides of Computer Science

Partial preview of the text

Download MATLAB Basics: Variables, Arrays, and Operations and more Slides Computer Science in PDF only on Docsity!

Chapter 2 MATLAB Basics

• Variables and Arrays

• Initializing Variables in

MATLAB

• Multidimensional Arrays

• Subarrays

• Special Values

• Displaying Output Data

• Data Files

• Scalar and Array

Operations

• Hierarchy of Operations

• Built-in MATLAB

Functions

• Introduction to Plotting

• Examples

• Debugging MATLAB

Programs

• Exercises

2.1Variables and Arrays

• Array is the fundamental unit of data in any

program.

• Arrays can be classified as either vectors or

matrices

Variable

• A MATLAB variable is a region of memory containing an

array that is know by a user-specified name.

• Begin with a letter , followed by any combination of letters ,

numbers , and the underscore (_) character.

• 63 characters

• Pick meaningful names and easy-to-remember for the

variables

• Spaces cannot be used in MATLAB variable names

• Case-sensitive: uppercase and lowercase letters are not the

same

• It is good practice to use only lower-case letters in variable

names.

  • Types of MATLAB variables:
    • double
      • consist of scalars or arrays of 64-bit double-precision floating-point numbers
      • real, imaginary , or complex values
      •  10 -^308 ~10^308 , with 15~16 significant decimal digits of accuracy
      • var = 10.5, var = 4i, var = 10 + 10i
    • char
      • consist of scalars or arrays of 16-bit values, each representing a single

character

  • comment = ‘This is a character string’; %1x26 character array
  • weakly typed
  • variables may be created at any time by simply assigning values to them.
  • strongly typed
  • the type of every variable must be explicitly declared in program before it

is used.

Initializing variables in assignment

statements

  • var = expression
    • expression is a scalar constant, an array, or

combination of constants, other variables, and

mathematical operations.

  • var = 40i;
  • var2 = var/5;
  • array = [1 2 3 4];
  • x = 1; y = 2;
  • Arrays are constructed using brackets [] and

semicolons.

  • row order
  • Individual values within a row are separated by blank

spaces or commas

  • The rows are separated by semicolons or new lines
  • [3,4] [1.0 2.0 3.0] [1.0; 2.0; 3.0]
  • [1, 2, 3; 4, 5, 6] [1, 2, 3 []

4, 5, 5]

  • The semicolon at the end of each assignment statement
    • It suppresses the automatic echoing of values
    • Speed
  • If an assignment statement without the semicolon, the

results of the statement are automatically displayed in the

command windows

e = [1, 2, 3; 4, 5, 6]

e =

1 2 3

4 5 6

  • Debugging

Initializing with shortcut expressions

  • colon operator
    • first:incr:last

x = 1:2:

x =

1 3 5 7 9

  • angles = (0.01:0.01:1.00) * pi;
  • transpose operator (‘)
  • Swaps the row and columns of an array that it is applied
  • f = [1:4]’; produce?
  • g = 1:4; produce?
  • h = [g’ g’]; produce?

Initializing variables with keyboard

input

  • input
    • user_entry = input('prompt')
    • user_entry = input('prompt','s')
    • in1 = input(‘Enter data: ');
    • in2 = input(‘Enter data: ','s');
  • see MATLAB Help

2.3 Multidimensional Arrays

(a) (b)

  • R o w One-Dimensional Array Tw o-Dimensional Array
  • R o w
  • R o w
  • R o w
    • R o w
    • R o w
    • Row
    • R o w - Col - Col - Col - Col
      • Col

Storing multidimensional arrays in

memory

  • MATLAB allocates

array elements in

column major order

1 2 3

4 5 6

7 8 9

10 11 12

(a )

a

(b )

Arrangement

in Computer

Memory

a( 1 , 1 )

a( 2 , 2 )

a( 1 , 2 )

a( 4 , 1 )

a( 3 , 1 )

a( 2 , 1 )

a( 4 , 3 )

a( 3 , 3 )

a( 2 , 3 )

a( 1 , 3 )

a( 4 , 2 )

a( 3 , 2 )

Accessing multidimensional arrays

with one dimension

a(5) will be 2

The end function

• Returns the highest value taken on by that

subscript

  • If end appears in different subscripts, it can

return different values within the same

expression.