Sunteți pe pagina 1din 34

Introduction To Matlab

Class 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

OptionalPresentationTitle

Introduction
Ask Questions!
Slides, notes, assignments, and other info will be at:

http://ruccs.rutgers.edu/matlab_course/

UnitName

OptionalPresentationTitle

Setup
Click Matlab

mkdir C:/MatlabClass/YourLastName
cd C:/MatlabClass/YourLastName

UnitName

OptionalPresentationTitle

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

UnitName

OptionalPresentationTitle

Two Options For Where To Issue


Commands/Instructions
In the command window
In a script (program) executed by the command window.

UnitName

OptionalPresentationTitle

Hello World
At the command prompt type:
my_var = hello world;
And press ENTER.

Note:The_characteristhe
shifteddash
SHIFT+
UnitName

OptionalPresentationTitle

Hello World

my_var = hello world;

Semicolonissoyoudont
outputtheresulttothe
screen.
Variable
Name
UnitName

Singlequotesdenotecharacterstring

OptionalPresentationTitle

Variables
num_oranges=3.5
num_apples=7

my_var = hello world;

Variable
Name
UnitName

helloworld

3.5

my_var

num_oranges

7
num_apples

OptionalPresentationTitle

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

UnitName

Yourcomputersmemory
VarName

Value

my_var

helloworld

num_apples

num_oranges

3.5

subject_name

johnsmith

OptionalPresentationTitle

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.

UnitName

OptionalPresentationTitle

Setting values
new_var=my_var
third_character=my_var(3)

thisis7
thisis3.5

fruit=num_apples+num_oranges
num_apples=6

UnitName

whatisthevalueoffruitnow?

thisisnow10.5

OptionalPresentationTitle

Changing around strings

Typing my_var(3) will return l

my_variscurrently
helloworld

my_var(3) = my_var(1)
Now what is the value of my_var?

UnitName

OptionalPresentationTitle

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)

UnitName

OptionalPresentationTitle

Nesting Functions
disp(length(my_var))

disp(length(helloworld))
disp(11)
11
UnitName

Whathappenshere?

OptionalPresentationTitle

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

UnitName

OptionalPresentationTitle

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

UnitName

OptionalPresentationTitle

%symbolmakesthislinecommentedout
Thismeansthatthecomputerwillignoretheselines
Theselinesarentforthecomputer,theyareforyou

% mix_strings.m
Nameoffile
%
% Displays the string hello world
% Then scrambles letters in that string and
% displays the scrambled string
%
% written by XYZ 3/6/2005

Description:
Whatdoesthis
filedo?

UnitName

Whowroteitandwhen?

OptionalPresentationTitle

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

UnitName

OptionalPresentationTitle

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

UnitName

OptionalPresentationTitle

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);

UnitName

OptionalPresentationTitle

Now run it
In the command window:
mix_strings

UnitName

OptionalPresentationTitle

loops
Used to repeat the same chunk of code many times

for i=1:5
disp(i)
end;

UnitName

OptionalPresentationTitle

mix_strings2.m
clear all;
my_var = hello world;
mixstr = xxxxxxxxxx;
for i=1:length(my_var)
mixstr(i) = my_var(i);
end;
disp(mixstr)

UnitName

OptionalPresentationTitle

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 integers
ceil(rand*7)

UnitName

OptionalPresentationTitle

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 -> wdolhrdlhlwe

UnitName

OptionalPresentationTitle

% 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 = 1:length(mixstr)
mixstr(i) = my_var(ceil(rand*length(my_var)));
end;
disp(mixstr);
UnitName

OptionalPresentationTitle

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]
UnitName

OptionalPresentationTitle

Vectors & Matrixes


vect = [1, 2, 5, 6, 3]
mat = [1, 5, 3; 2, 1, 5; 7, 9, 0]

Vect

mat 1

2
7
UnitName

2
5
1
9

3
5
0

mat=[1,5,3;
2,1,5;
7,9,0]
5

note:vectorsarebasically
1dimensionalmatrixes

OptionalPresentationTitle

Getting Values out of Vectors & Matrixes


Vect

vect(3)returns5
mat

1
2
7

5
1
9

3
5
0

mat(3,1)returns7
Note:togetacellyouuse(ROW,COLUMN)
UnitName

OptionalPresentationTitle

vector as a 1D matrix
vect = [1;
2;
3]
vect2 = vect

1
2
3

UnitName

OptionalPresentationTitle

transposing mat
mat=

1
2
7

5
1
9

3
5
0

mat=

1
5
3
UnitName

2
1
5

7
9
0

ifyousett_mat=mat
t_mat(3,1)isequaltomat(1,3)
Rowsandcolumnsareswapped
whenyoutransposeamatrix

OptionalPresentationTitle

performing operations
vect + 3
vect + vect
vect - 3
vect + [5, 4, 3]
mat+mat

UnitName

OptionalPresentationTitle

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

UnitName

OptionalPresentationTitle

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
UnitName

S-ar putea să vă placă și