Learning MATLAB by Doing MATLAB
Learning MATLAB by Doing MATLAB
Learning MATLAB by Doing MATLAB
This
3 3 identity matrix
diagonal matrix
geht auch mit Spaltenvektoren
4 4 matrix of all ones
2 3 matrix of all ones
block matrix
transpose of C
MATLAB considers w to be a row vector. The third element of w is set to 5.
row vector with entries from 0 to 2 in steps of 1/3
i runs from 1 to 10. (MATLAB waits for the final end kommt!)
the index i = 0 is not possible here, since vectors have no 0th element!
is partly based on lecture notes by Christian Mehl and Andreas Steinbrecher, both TU Berlin.
2. Simple Operations
>clear
>A=[1 2 3;2 1 0]
>B=[2 2;1 0;0 1]
>C=[0 1 0;5 1 3]
>size(A)
>[m,n]=size(A)
>m=size(A,1)
>b=[2 1 3]
>x=[2;1;3]
>A*B
>A*C
>A
>C
>A*C
>diag(A*C)
>whos
>D=A+C
>E=A+B
>E=A-B
>g=A*x
>g=A*b
>A
>b
>B
>f=b*B
>C=[1 2 3]
matrix multiplications
error, dimensions do not fit!
A CT
returns the diagonal of a matrix
ans (for answer) is the is the unnamed output
matrix addition
error, dimensions do not fit!
E = A BT
matrix times vector
error!
3. Matrix Manipulations
>clear
>A=[1 2;3 4]
>A(3,2)=7
>A(1:2,2)
>A(3,1:2)
>B(3:4,3)=[5;6]
>C(4:5,4)=A(1:2,2)
>B(:,3)
3rd column of B
>d=C(1,:)
1st row of C
>E=[1 2 3;4 5 6;7 8 9;10 11 12]
>E(1:2:4,3)
(1:2:4,3): Picks every second element in column 3 from 1 to 4.
>F=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15]
>G=F(1:2:3,1:2:5)
Picks every second element in columns 1,3,5 from 1 to 3.
>G=F(1:2:end,1:2:end)
Alternative notation if you forgot the dimensions.
>H=[1 3;9 11]
>H^(-1)
The inverse.
>inv(H)
Also the inverse.
>H\A(1:2,1:2)
Always use the backslash for computing H 1 A. Dont use inv(H)*A!
>det(H)
The determinant.
>b=[99 100 101]
>F(1,1:3)=b
>A
>A(1,1:3)=b
Elements (2, 3) and (3, 3) are added.
4. Subprograms, m Files
For the following subprograms you have to create files with the name of the function appended by .m, for example
test1.m. (You may use any editor to this, MATLAB comes with a built-in editor which also offers debugging.)
The m files have to be in the current working directory. Try help pwd and help cd for more infos about working
directories.
% This is my first m file.
% You can add comments behind %.
% It is called test1.m and computes the sum of all elements of a matrix A.
function y=test1(A)
[m,n]=size(A);
y=0;
% Init
for i=1:m
% i runs from 1 to m
for j=1:n
% j runs from 1 to n
y=y+A(i,j);
end
Save under test1.m.
end
>clear
>help test1
>A=[1 2;3 4]
>s=test1(A)
% The program test2.m computes the sum and the product of all elements of a matrix A,
% as well as the trace if the matrix is square.
function [y,p,t]=test2(A)
[m,n]=size(A);
summe=0;
p=1;
t=0;
for i=1:m
if (m==n)
% if m = n, then ...
t=t+A(i,i);
end
for j=1:n
y=y+A(i,j);
p=p*A(i,j);
end
end
Save under test2.m.
MATLAB functions can have several input variables (e.g., function ausgabe=test3(A,B,C)) or require no input/output at all (e.g., function []=test4() or you just omit the whole function declaration, call with test4).
Besides the for loop, there is also a while loop. See help while.
5. Graphics
>for i=1:10, x(i)=i/10;
>plot(x,y)
>plot(x,z)
>clf
>plot(x,y)
>hold on
>plot(x,z)
>plot(x,2*z,r)
>plot(x,y+z,g*)
>hold off
>plot(x,y-z,k+)
>help plot
>title(My plot)
>xlabel(x axis)
>ylabel(y axis)
>axis([0,20,-5,50])
>box
>grid
>clf
>subplot(3,2,1)
>plot(x,y)
>subplot(3,2,2)
>plot(x,z,k)
>subplot(3,2,5)
>plot(x,z+y,mo)
>hold on
>plot(x,z,k)
>subplot(3,2,1)
>plot(x,z,k)
>subplot(3,2,4)
>title(leer)
>subplot(3,1,2)
>plot(y)
>orient tall
>help orient
>print -dps test1.ps
>help print
We have only 3 subplots, one in each row. The 2nd is now active.
Creates a pos file test1.ps of this plot (more options in the menu File/Save As..)
Turn on Tools/Edit Plots and right click to change properties of the plots. For inclusion of plots in presentations and
papers it is often a good idea to increase the font sizes and thicken the lines.
6. Other useful commands
load
save
blkdiag
kron
A(:)
.* ./
chol
cond
eig
hess
norm
qr
schur
svd