Matlab Summary
Matlab Summary
Matlab Summary
• To create a matrix:
1 2 3
A = [1 2 3; 4 5 6]; makes
4 5 6
• This also works:
A = [1,2,3;4,5,6]; or [1 2 3
4 5 6]
• A = [a b]; gives 1 2 3 4 5 6
1 2 3
• A = [a; b]; gives
4 5 6
• A = [0:5:20]; makes 0 5 10 15 20
0
5
A = [0:5:20]’; makes 10
15
20
Using the : symbol
• Access format: Similar, b
1 2 3 4 2
5 6 7 8 6
A= A(:, 2) gives 2nd column
9 10 11 12 10
13 14 15 16 14
3 4
A(1:2, 3:4) gives 1-2 row, 3-4 column submatrix
7 8
end
Programming Logic
• if, else statements:
if (a > 1)
blah
else
blahblah
end
• for statements can be used too:
for i=1:n
moreblah
end
• Similar behavior for repeat, until, while, etc.
Function parameters
function [ output1, output2 ] = Silly( input1, input2)
• To input values, use the as many arguments after the
function name as you need, then use them in your
program.
some_value = input1*input2;
output1 = some_value;
output2 = 15.7;
end
Calling Functions
• Note that the type of input1, input2 is not set anywhere.
Can be scalars, vectors, matrices…
• To call this function with 2 return values, do:
[a, b] = Silly(5, 7);
• Use F5 to run the script. Using the name of the script from
the command line works too.
• Use F9 to run the current selection.
• plot(x1, y1, ':bo', x2, y2, '-r.') will plot two curves, one as a
blue dotted line with circles at each point, the other red
continuous with dots.
Plotting cont.
• Look for ”Linespec” in the MATLAB documentation to find
other codes for line colors, markers, etc.
• To combine plots: use hold on after the first one and hold
off after the last plot.
hold on
plot (x1, y1, ':bo')
plot (x2, y2, '-r.')
hold off