参考资料1 Matlab Tutorial
参考资料1 Matlab Tutorial
Contents
Declaring a Variable
Functions
Plotting Results
Declaring a Variable
The first thing we'll do is make a variable. It's pretty easy actually; just use the
equals sign.
x=4
x =
x =
y =
6
z =
MATLAB's real power is in dealing with vectors and matrices. To utilize that
power, first we must make a vector and a matrix. Let's make a vector first.
x = 1:11
x =
1 2 3 4 5 6 7 8 9 10 11
Note that we have made a vector that goes from 1 to 6 with stepsize 1. Pretty
x =
1 3 5 7 9 11
x =
1 4 3 2 5 6
That gives us a row vector, but what about a column vector? Well, just transpose
the row vector. To transpose a MATLAB vector, use the single quote.
x = x'
x =
1
4
3
2
5
6
x =
1 2 3 4
5 6 7 8
Note how I used the semicolon in the brackets to put the rows on top of each
other. Suppose I wanted to put two columns together instead; then, I use a
comma or a space
y = [1 2 3 4]'
z = [5 6 7 8]'
x = [y, z]
x = [y z]
y =
1
2
3
4
z =
5
6
7
8
x =
1 5
2 6
3 7
4 8
x =
1 5
2 6
3 7
4 8
are 1xn matrices and columns are nx1 matrices. That will make the manipulation
x =
-4
-4
-4
-4
... as long as the elements are the same. One issue is multiplication and division.
z =
6 6
14 14
is not just element‐wise; it's real matrix multiplication. But what if we just want
z =
2 4
6 8
z =
1 4
9 16
We also have some special vectors that can be made via functions:
x = zeros(3)
y = ones(3,1)
z = rand(4,2)
x =
0 0 0
0 0 0
0 0 0
y =
1
1
1
z =
0.8147 0.6324
0.9058 0.0975
0.1270 0.2785
0.9134 0.5469
The last one is very important, as it allows us to make a matrix of values that are
distribution, use
z = randn(4,2)
z =
3.5784 0.7254
2.7694 -0.0631
-1.3499 0.7147
3.0349 -0.2050
Functions
Also, the last example shows us that calling functions are easy. Given a function
name you can figure out how the function works by the help command
help interp1
interp1 - 1-D data interpolation (table lookup)
vq = interp1(x,v,xq)
vq = interp1(x,v,xq,method)
vq = interp1(x,v,xq,method,extrapolation)
vq = interp1(v,xq)
vq = interp1(v,xq,method)
vq = interp1(v,xq,method,extrapolation)
pp = interp1(x,v,method,'pp')
This tells us how the interp1 function works. Pretty cool function, eh? If you
want to see all the description and examples of each syntax, one click on doc
interp1.
ans fs m t x y z
and to see the details (how big the variables are, etc.) use
whos
which shows the current variables in your "workspace". We could also click into
At some point you guys will be making your own functions. To do that, you make
a new M‐file and name it by the function name. Let's say our function is called
syed; then the new file will be syed.m ... let's let it do something simple: function
for a matrix it sums up each row. Since the .^2 squares the elements, we get a
sum‐of‐squares! Note first the keyword function. If that keyword is not there,
then we have a script. What's the difference? A script is just a list of commands
that are run. A function has inputs and outputs. So if you want a script (generally
the "main" file for your work will be a script) then make sure the first word is
not function. When you make a script, it is also an M‐file. For example, this
tutorial was created in an M‐file called tutorial.m and I called it in the command
line just by typing the M‐file name "tutorial" ... make sure the M‐file is in your
path. You can see your path at the top of MATLAB or by the command
pwd
ans =
C:\Users\ROG\Desktop\worksapce\matlab
Your current directory is the directory you're working in, so generally you start
by changing your directory using the command cd
cd C:\Users\ROG\Desktop\worksapce\matlab
The next step to complicate things is loops. To make a for loop use the for
command:
for m = 1:10
x(m) = m^2;
end
Note the syntax: after the for loop you give the iterating variable (don't use i or
j because in MATLAB, they are the square root of ‐1). Then you set it equal to a
ans =
ans =
16
ans =
ans =
49
So for loops are cool right? WRONG. At all costs, do not use for loops. They are
super‐slow and basically only for the most necessary situations. Take the
x =
1 4 9 16 25 36 49 64 81 100
and get what we want much faster. Always try to vectorize your code. One way
to help you out is this tool, which takes any matrix and turns it into a vector:
x = rand(3,3)
x(:)
x =
ans =
0.4218
0.9157
0.7922
0.9595
0.6557
0.0357
0.8491
0.9340
0.6787
alltheplayers =
threshold =
0.7460
selectplayers =
0 0 0 0 1
we compare the first row of matrix alltheplayers with a single variable threshold,
If you have to use a for loop, try to initialize the variable that's being updated.
to prevent major time issues due to having to constantly add size to the vector.
What's up?
Note that the "end" ends the if loop. disp displays a string; note how you use
Plotting Results
Whoa, wait. What was that percent sign and green thing? You probably guessed
that it was a comment. The percent sign is MATLAB's version of the // for C. Use
it often, and use it well. Please. For our sanity. And yours. We want comments
that are good and frequent. They help; I promise. Ok, back to plotting. The plot
goes first, followed by the y‐axis. This plot is pretty lame. If x were filter
Well, I'm a command‐line guy, so I use it a lot. Let's start with annotation:
title('INSERT TITLE HERE');
xlabel('Descriptive name of x variable');
ylabel('Descriptive name of y variable');
Please add labels to all your plots, k? K. K? K. To change the color or the dots,
use help plot to get a list of things to add as the third argument to the plot
function. So I can do
plot(t, x, 'rp-.');
to make the plot red with pentagrams at the points and a dot‐dashed line. As
you guys get better you guys can mess around with the Figure window and make
really good plots. We find it very important that you make good plots because
good plots mean good work :) Suppose you wanted to plot two signals on the
hold off command. If you would rather have the plots side by side on the same
the figure. So we have 2 rows and 1 column, which means one plot on top of
the other. Then the numbering is done row‐wise first, so that 1 means the first
axis. 2 means the 2nd axis on that row, etc. We can change
concatenation,
plotting results.
This should get you a decent head start into MATLAB. Here's a list of other useful
sum/max/min
dlmread/dlmwrite
size/length
fprintf
logical
xor
ceil/floor/round
polyfit
polyval
interp1