Introduction To Matlab Class 1 - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Introduction To Matlab Class 1

Description:

Introduction To Matlab Class 1 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD Double click the matlab icon When prompted click Skip , then type: – PowerPoint PPT presentation

Number of Views:221
Avg rating:3.0/5.0
Slides: 35
Provided by: Robert2282
Category:

less

Transcript and Presenter's Notes

Title: Introduction To Matlab Class 1


1
Introduction To MatlabClass 1
  • Instructors Hristiyan (Chris) Kourtev
  • and Xiaotao Su, PhD
  • Double click the matlab icon
  • When prompted click Skip, then type
  • mkdir C/MatlabClass/YourLastName
  • cd C/MatlabClass/YourLastName

2
Introduction
  • Ask Questions!
  • Slides, notes, assignments, and other info will
    be at
  • http//ruccs.rutgers.edu/matlab_course/

3
Setup
  • Click Matlab
  • mkdir C/MatlabClass/YourLastName
  • cd C/MatlabClass/YourLastName

4
Programming
  • Matlab has its own programming language
  • What is a program?
  • A series of instructions that the computer
    follows
  • Also referred to as Code or a Script

5
Two Options For Where To Issue Commands/Instructio
ns
  • In the command window
  • In a script (program) executed by the command
    window.

6
Hello World
  • At the command prompt type
  • my_var hello world
  • And press ENTER.

Note The _ character is the shifted
dashSHIFT -
7
Hello World
  • my_var hello world

Semi-colon is so you dontoutput the result to
the screen.
Single quotes denote character string
Variable Name
8
Variables
num_oranges 3.5
  • my_var hello world

num_apples 7
7
hello world
3.5
my_var
num_apples
num_oranges
Variable Name
9
Memory
Your computers memory
  • Each of the values are stored in memory
    associated with the variable name
  • The who command will list the current variables
    in memory
  • Typing my_var will return the value of that
    variable

Var Name Value
my_var hello world
num_apples 7
num_oranges 3.5
subject_name john smith



10
Setting values
  • As you have seen you set the value of a variable
    with the sign
  • You can also set a variable equal to another
    variable or the output of an operation on a
    variable.

11
Setting values
new_var my_var
third_character my_var(3)
this is 7
this is 3.5
fruit num_apples num_oranges
this is now 10.5
num_apples 6
what is the value of fruit now?
12
Changing around strings
my_var is currentlyhello world
  • Typing my_var(3) will return l
  • my_var(3) my_var(1)
  • Now what is the value of my_var?

13
Calling Functions
  • A function is a program that you call on to do
    some sort of action
  • length(hello world)
  • length(my_var)
  • disp(my_var)

14
Nesting Functions
  • disp(length(my_var))

What happens here?
disp( length(hello world) )
disp(11)
11
15
Your first program
  • What is pseudo-code and why we need it
  • Typing commands one at a time isnt very
    efficient, especially when you want to do the
    same ones repeatedly
  • so we create a program to do it
  • This program will be called mix_strings

16
Comments
  • Comments are descriptions of what is happening in
    your code.
  • They are need to follow what is happening for
    debugging purposes
  • At the beginning of a script you put in a type of
    comment called a header

17
symbol makes this line commented out This
means that the computer will ignore these lines
These lines arent for the computer, they are for
you
  • mix_strings.m
  • Displays the string hello world
  • Then scrambles letters in that string and
  • displays the scrambled string
  • written by XYZ 3/6/2005

Name of file
Description What does this file do?
Who wrote it and when?
18
So what is the header good for?
  • Save the file
  • At the command line type
  • help mix_strings
  • All matlab programs have headers. Find out
    information on disp, pause, and length

19
What else?
  • lookfor can be used to search for programs that
    have certain words in their headers.
  • lookfor string
  • lookfor random

20
Back to the program
  • clear all
  • my_var hello world
  • mixstr my_var
  • mixstr(3) my_var(1)
  • mixstr(1) my_var(3)
  • disp(mixstr)

21
Now run it
  • In the command window
  • mix_strings

22
loops
  • Used to repeat the same chunk of code many times
  • for i15
  • disp(i)
  • end

23
mix_strings2.m
  • clear all
  • my_var hello world
  • mixstr xxxxxxxxxx
  • for i1length(my_var)
  • mixstr(i) my_var(i)
  • end
  • disp(mixstr)

24
rand and ceil
  • rand will create a random number from 0 to 1
  • ceil will round up any number to the next integer
  • Together you can create random integersceil(rand
    7)

25
task 1
  • Replace each x in mixstr with a random character
    from my_var.
  • Start with the first x, then the second x
  • hello world
  • xxxxxxxxxxxxx
  • Result could be -gt wdolhrdlhlwe

26
  • mix_strings3.m
  • Displays the string 'hello world, then
    scrambles letters in that string
  • and displays the scrambled string
  • written by Chris Kourtev 7/12/2010
  • clear all
  • my_var 'hello world'
  • mixstr 'xxxxxxxxxxxxx'
  • disp(my_var)
  • for i 1length(mixstr)
  • mixstr(i) my_var(ceil(randlength(my_var)))
  • end
  • disp(mixstr)

27
Vectors Matrixes
  • A string is a list of characters
  • You can also have lists of numbers using vectors
    and matrixes
  • vect 1, 2, 5, 6, 3
  • mat 1, 5, 3 2, 1, 5 7, 9, 0

mat 1, 5, 3 2, 1, 5 7,
9, 0
28
Vectors Matrixes
  • vect 1, 2, 5, 6, 3
  • mat 1, 5, 3 2, 1, 5 7, 9, 0

mat 1, 5, 3 2, 1, 5 7,
9, 0
1 2 5 6 3
Vect
mat
1 5 3
2 1 5
7 9 0
note vectors are basically 1 dimensional matrixes
29
Getting Values out of Vectors Matrixes
Vect
1 2 5 6 3
vect(3) returns 5
mat
1 5 3
2 1 5
7 9 0
mat(3,1) returns 7
Note to get a cell you use (ROW, COLUMN)
30
vector as a 1D matrix
  • vect 1
  • 2
  • 3
  • vect2 vect

1
2
3
1 2 3
31
transposing mat
mat
1 5 3
2 1 5
7 9 0
if you set t_mat mat t_mat(3, 1) is equal to
mat(1,3) Rows and columns are swapped when you
transpose a matrix
mat
1 2 7
5 1 9
3 5 0
32
performing operations
  • vect 3
  • vect vect
  • vect - 3
  • vect 5, 4, 3
  • matmat

33
multiply and divide
  • Dont forget to put a dot before multiplication
    and division symbols
  • vect.3
  • mat.0.5
  • vect./2
  • vect2 2 2 4
  • vect.vect2
  • vect./vect2

34
task 2
  • Create a 6(rows)x4(columns) matrix filled with
    random integer values between 1 and 20
  • Create a zeroes matrix of the same dimensions
  • Replace each zero with a random number from the
    other matrix (as we did in the mix_strings
    program)
  • Display each value in the new mixed matrix
  • Hint 1 You will need to use nested loops
  • Hint 2 Look up how to use the zeros command
  • Advanced task Design your program so that it
    can work with matrices of any dimension just by
    changing the number of rows and column. Hint
    Look up the size command
Write a Comment
User Comments (0)
About PowerShow.com