Sie sind auf Seite 1von 4

1

LAB #1
Brief review of MATLAB instructions known from previous courses
MATLAB use:

From prompt window


Script file (file.m)

Vector and matrix assignment:

X=[1,2,3,4]; or X=[1 2 3 4]; line vector


Otherwise: X=[xmin:increment:xmax];
X=[1;2;3;4] or X=[1 2 3 4]; column vector
A=[1 2; 3 4; 5 6]; 3 2 matrix
Special matrices:
ones(m,n) (m n) matrix all elements are = 1
zeros(m,n) (m n) matrix all elements are = 0
eye(n) (n n) identity matrix
diag(X, m) creates a matrix with the X vector placed on the m-th diagonal and
zero elsewhere. The m-th diagonal means the main diagonal if m=0, the first upper
diagonal if m=+1, the first lower diagonal if m=-1, ...

Matrix operation: if A e B are two matrices with the same size:

A+B sum
A-B difference
A*B scalar product
A.*B product (element by element)
A./B ratio (element by element)
A^2 A*A (scalar product)
A.^2 square (element by element)
max(A) o min(A) MATLAB builds a vector containing the maximum (or
minimum) element of each column.
abs(A) absolute value of (A).
sum(A,p) creates a row vector whose elements are sum BY COLUMN of the
elements of A (if p=1) or a column vector whose elements are the sum BY ROW of
the elements of A (if p=2)

Main graphical commands:

plot(x,y,type_of_line or type_of_point)

Main I/O commands:

disp(print_this_sentence_on_the_screen);
load filename.dat or load filename.mat
save filename [variable1, variable2, ]

Main pre-defined functions:

sin(x)
cos(x)
exp(x)
log(x) natural logarithm

CM Lab #1

Oct. 4, 2013

Other useful commands:

clear('pippo') MATLAB clears the variable pippo form memory


close(xx) MATLAB close the graphical window # xx
clear all / close all MATLAB clears all the variables and closes all the graphical
windows RECOMMENDED AT THE BEGINNING OF ANY SCRIPT
% before comments in the script files (ctrl+r comment, ctrl+t uncomment)
size(A), length (B) dimension (rows, columns) of matrix A, number of elements of
vector B
tic/toc start/stop an internal clock. Useful to time code execution
clc MATLAB clears all the lines in the command prompt
help ...(name of a predefined MATLAB function) shows the full description of the
function: inputs, outputs, how it works
find(logical expression) returns the index of the elements in the vector/matrix
satisfying the logical expression written in input.
cd move into directory

CM Lab #1

Oct. 4, 2013

EXERCISES
1) Assign the variables A=20 and B=5
a. Compute the sum S and the product P of A and B.
b. Compute A^2.
c. Change the value of A to 10 and check the values of A and B in the workspace.
d. Clear the sum S and recomputed it with the new values.
e. Recompute also the product P without clearing it before.
f. Try to assign the variable named 3A=24: what happens?
g. Clear the command prompt.
h. Save the sum and the product in a file named ES1_yoursurname_
BaseOperations.properextension. Try with both .mat and .dat files.
i. Create the folder Lab1 in d:\ and save the same file in that folder.
j. Define the variable a=8 and check the value of variable A.
k. Clear all the variables.
l. Load the file and check the variables values.

2) Assign the following matrices:


A=[2 5

3];

B=[3 6

4];

a) Which are the dimensions of A?


b) Whats the result of: A'?
c) Whats the result of: A*B? And of A*B'?
d) Whats the result of: A.*B?
e) Whats the result of: A.^B?
f) Whats the result of: C=[A' B' A' B'] ?
g) Whats the result of: C(3,2)?
h) Whats the result of: D=[A; B; A; B]?
i) Whats the result of: D(2,3)?
l) Assign the matrix W=[D D;C C] and the vector w=[1 3 5 7]. Whats the value of
q=W(w,2)? And z=W(w,:)?

3) Assign the matrix:


a) Assign the matrix: X =[ 1
2
...
6
4
5
...
9
0.1
0.2
...
0.6
0.1
0.3
...
1.1]
(Try to use an efficient MATLAB notation).
b) Which are the dimensions of X?
c) Find the positions where there are values larger than 5.

CM Lab #1

Oct. 4, 2013

4) Using the special matrices ones, zeros and eye, assign the matrix:
A=[0
0
1
1
1
2
2
2

0
0
1
1
1
4
4
4

1
2
3
0
0
0
2
0

2
1
2
0
0
0
0
2

3
2
1
0
0
0
0
0

1
0
0
3
3
6
7
0

0
1
0
3
3
6
0
7

0
0
1
3
3
6
0
0]

Q=[1
2
3
4
1
9
6
8
4

2
3
4
6
3
7
8
1
6

3
4
5
8
5
5
2
3
4

4
5
6
1
7
3
9
5
6

5
6
7
3
9
1
7
7
4

6
7
8
5
2
8
5
9
8

7
8
9
7
4
6
3
2
6

8
9
1
9
6
4
1
4
8

5) Assign the matrix Q:


9
1
2
2
8
2
4
6
6]

And then:
a) Compute the sum of the terms in the odd columns.
b) Compute the sum of the terms in the main diagonal.
c) Compute the sum of the outermost frame elements, which are not 4.
d) Compute the sum of all the matrix terms, excluding those equal to 9, close to 7.

6) We want to plot the function y=sin(x) with 0x50, using the MATLAB pre-defined
function sin(x).
a) Assign a vector x containing all the points from 0 e 50, with increment 0.01, and
compute the y vector with an array-smart command.
b) Plot y as a function of x.

CM Lab #1

Oct. 4, 2013

Das könnte Ihnen auch gefallen