Vous êtes sur la page 1sur 4

Introduction to Matlab

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

Help and Online Documentation


This document provides a basic introduction to matlab and the best way to make good use of
it is to run its examples in your computer. After you grasp the basic facts about matlab you
can use the help features and online documentation provided by the supplier of matlab:
There are several different ways to access online information about matlab functions:
Type the following and hit the return key
help

to determine the syntax and behaviour of a particular function. For example to


see how to use the plot function, type help plot and matlab will give you the
syntax of the command and it will describe how to plot graphs
helpwin

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

Addition and Subtraction:


A + B
A - B
2 + a
(A + B) * (a - 4)

Division: A * (B inverse)
A / B

Division: (A inverse) * B
A \ B

Inverse:
inv (A)

Power:
B ^ 3

Complex Conjugate Transpose: (This is same as Transpose for real matrices)


A'

Some Special Matrices


Identity Matrix:
A = eye(5)

creates A which is 5x5 identity matrix.


Zero Matrix:
B = zeros(4,2)

creates B which is 4x2 zero matrix.


Matrix of Ones:
C = ones(3,3)

creates C which is 3x3 matrix of ones.


Row Vector Whose Components Increase Arithmetically:
u = -2:10

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)

Diagonal Matrix with a Given Vector on the Diagonal:


D = diag(u)

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)

Normally Distributed Random Elements:


F = randn(100,10)

More on Matrices
More on the use of Colon Operator:
F(1:k,j)

refers to the first k elements of the column j of F.

sum(A(2,:))

calculates the sum of elements in the second row of A.

sum(A(1,1:2))

calculates the sum of the first two elements in the first row of A.

sum(A(:,end))

calculates the sum of the last column of A.

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.

Reading-Writing data with matlab


The area of memory accessible from the command line is called workspace. As you
create variables, they are stored in the workspace for that session. If you want to save
the variables in the workspace into disk then you type save. This command saves all
the variables in the workspace into the binary file matlab.mat. If you want to save them
in a particular file then type save filename. You can also save only certain variables
by specifying the variable names after the filename. To delete all existing files from the
workspace enter clear. To load a previously saved workspace, enter load. You can
also use these commands to save and load ascii data: save -ascii and load -ascii
To read an ascii file with some flexibility you can use the textread command:
reads the variables A, B, C, ... from
file FILENAME where the content types of these variables (i.e. integers, strings,
floating numbers are given by FORMAT. N tells matlab how many observations are to
be read for the variables A, B, C, ... The FILENAME and FORMAT are strings so
while enetring their valiues you should enclose them in single quotes. Here is an
example: Let's say you have a text file mydata.dat containing the following lines:

[A,B,C,...] = TEXTREAD(FILENAME,FORMAT,N)

tommy 32 male 78.8


sady 3 female 88.2
alex 27 male 44.4
saul 11 male 99.6

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,age,sex,score] = textread('mydata.dat','%s %d %s %f',2)


then matlab will create 4 variables each consisting of 2 values. Note that the we force
matlab to read only 2 observations although there are 4. If you do not specify the N
field in textread then matlab reads all values.
name,

the first term in the FORMAT field, %s indicates that it is string-type


the second term %d indicates that it is integer-type
male, the third term %s indicates that it is string-type
score, the last term %f indicates that it is floating-type.
Since we did not put ";" at the end of the command, matlab is going to show us the
result:
age,

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

Vous aimerez peut-être aussi