Documente Academic
Documente Profesional
Documente Cultură
Class 1
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
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
Semicolonissoyoudont
outputtheresulttothe
screen.
Variable
Name
UnitName
Singlequotesdenotecharacterstring
OptionalPresentationTitle
Variables
num_oranges=3.5
num_apples=7
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
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
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
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
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
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
mat=[1,5,3;
2,1,5;
7,9,0]
UnitName
OptionalPresentationTitle
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
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
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