Académique Documents
Professionnel Documents
Culture Documents
1 of 4
https://blackboard.tudelft.nl/bbcswebdav/pid-1859516-dt-content-rid-...
Introduction to Matlab
Help and Online Documentation
Matrices in matlab
Entering Matrices
Arithmetic with Matrices
Some Special Matrices
More on Matrices
Reading-Writing data with matlab
Graphics
to get a general help menu window about all the topics for which help is
available. If you type helpwin plot then you will see a new window with the
description of the plot function
lookfor keyword
to get a listing in the command window for all commands whose descriptions
include the keyword. Let us say you want to know what commands matlab has
for writing data into disk. Then you may want to type lookfor write
helpdesk
to display the Help Desk page in a web browser, providing direct access to a
comprehensive library of online help, PDF-formatted documentation, troubleshooting information and the MathWorks web site
Matrices in matlab
matlab is an efficient computer programming language if the objects in your programs are
matrices and vectors. It stores the numerical objects as matrices and it allows the user to
create and manipulate matrices in a wide variety of ways. It even treats a scalar as a 1 by 1
matrix. So if your programming involves matrices (or even if it does not involve matrices
you can still take advantage of matlab) you will find matlab to be easily programmable and
very helpful for your computations.
Entering Matrices
One of the ways you can enter a matrix into matlab is to list its entries explicitly:
Start with [ , and stop with ]
To indicate the end of rows, either use a semicolon ; , or hit the enter key
Put commas , or blanks between elements of a row
Now let's define some matrices:
a = [1, 0, 2; 1, 1, 1; 2, 2, 0]
A = [1 2 3
4 4 1
0 9 3 ]
B = [2 4, 6
1 ,3 5;
3 6 9
]
Note the flexibility in the definitions above. What you have to do is just to make sure
you comply with the three rules mentioned above.
20-2-2013 8:23
Introduction to Matlab
2 of 4
https://blackboard.tudelft.nl/bbcswebdav/pid-1859516-dt-content-rid-...
Also note that Matlab is case sensitive, so a and A are different objects.
If you do not put a semicolon after a command then Matlab displays the output of that
command. In the above examples we did not type ;, hence Matlab displays the matrix
that is formed each time. If we type ;, say, after the definition of A, then Matlab stores
A, but does not display it.
The result of the last command that is executed is stored in the variable named ans.
You can use ans as any other variable.
Arithmetic with Matrices
Multiplication:
A * B
a * 2
Division: A * (B inverse)
A / B
Division: (A inverse) * B
A \ B
Inverse:
inv (A)
Power:
B ^ 3
creates a row vector (of dimension 1x13) whose first element is -2 , last element
is 10 and all the elements in between increase one by one.
Row Vector Whose Components Change by Non-unit Steps:
v = 1:-.1:-1
creates a row vector (of dimension 1x21) whose first element is 1 , last element
is -1 and all the elements in between decrease by one-tenth.
Row Vector with Linearly Spaced Entries:
20-2-2013 8:23
Introduction to Matlab
3 of 4
https://blackboard.tudelft.nl/bbcswebdav/pid-1859516-dt-content-rid-...
w = linspace(0,pi,10)
creates a row vector (of dimension 1x10) whose first element is 0 , last element
is pi and all the elements in between are equally spaced and, there are 10 total
entries. So if you use linspace(p,q,n), then matlab creates a vector of size n
whose elements are equally spaced and whose first and last elements are p and q
respectively.
Row Vector with Logarithmitically Spaced Entries:
h = logspace(1,100,11)
creates a diagonal matrix D (of dimension 13x13) with the vector u on the
diagonal.
Column Vector of the Diagonal Elements of a Given Matrix:
diag(D)
displays the diagonal of the matrix D placed in a column vector. Note that the
vector is not stored anywhere other than ans.
Uniformly Distributed Random Elements:
E = rand(5,6)
More on Matrices
More on the use of Colon Operator:
F(1:k,j)
sum(A(2,:))
sum(A(1,1:2))
calculates the sum of the first two elements in the first row of A.
sum(A(:,end))
Note that if : is used alone then it refers to the whole row (or column).
Hence sum(A(:,:)) statement is equivalent to sum(A) statement. Note that
sum(A) produces a row vector in which the sum of each column of A is stored.
[A,B,C,...] = TEXTREAD(FILENAME,FORMAT,N)
If you type
20-2-2013 8:23
Introduction to Matlab
4 of 4
https://blackboard.tudelft.nl/bbcswebdav/pid-1859516-dt-content-rid-...
name =
'tommy'
'sady'
age =
32
43
sex =
'male'
'female'
score =
78.8000
88.2000
Graphics
To plot y against x type plot(x,y)
If you want to graph a function say sin(x), then you may type the following:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
20-2-2013 8:23