4-Matlab_Lecture
4-Matlab_Lecture
Hamdullah Yücel*
∗
Scientific Computing, Institute of Applied Mathematics
Lecture I
Basic Commands and Syntax, Arrays and Matrices
Experiment
Theory Computational
Science
Computational Science now constitutes what many call the third pillar of the scientific
enterprise, a peer alongside theory and physical experimentation.
Mathematics
SC
Computer
Science Natural
Sciences
Scientific Computing
= Computational Science
= Scientific Computation
= Computational Mathematics
Computational Computational
Biology Mechanics
Uncertainty Adaptive
Quan- Mesh
Portfolio
tification Generation
Optimization Algorithmic
Computational
Trading
Mathematics
& Simulation SCIENTIFIC
PDE-
Constrained
COMPUTING Computational
Optimization
Finance
High
(Non)Linear
Performance
Programming
Computing
Big Data
Data Science
Operations
Research
Statistical
Learning and
Data Mining
http://iam.metu.edu.tr/scientific-computing
https://www.facebook.com/SCiamMETU/
1 Introduction to MATLAB
3 Operators
4 Sparse Matrices
3 Operators
4 Sparse Matrices
Graphics
MATLAB has extensive facilities for displaying vectors and matrices as graphs,
as well as editing and printing these graphs. It also includes functions that
allow you to customize the appearance of graphics as well as build complete
graphical user interfaces on your MATLAB applications.
External Interfaces
The external interfaces library allows you to write C and Fortran programs that
interact with MATLAB.
Command Window: Here you can give MATLAB commands typed at the prompt, >>.
Workspace: Shows what variable names are currently defined and some info about their
contents.
who
returns the variables in your workspace
whos
returns the variables in the workspace with additional info (size, dimensions)
Try typing why in the command window. You will see that MATLAB is also a
Philosopher!
3 Operators
4 Sparse Matrices
Most variables you will deal with will be vectors or matrices of doubles or
chars
Other types are also supported: complex, symbolic, 16-bit and 8-bit integers,
etc.
H. Yücel (METU) MATLAB Lecture I 17 / 46
Variables
Naming Conventions
Have not to be previously declared
Variable names can contain up to 63 characters
To create a variable, simply assign a value to a name
>> var1 = 1903;
>> myStrings = ’merhaba’;
Variable names
first character must be LETTER
after that, any combination of letters, numbers and
allowable: NetCost, Left2Pay, X3, BJK1903
not allowable: Net-Cost, 1903BJK, %x, @sign
>> a = 1903
>> c = 2.4*24-4*a
>> h = 22/7;
linspace()
>> a = linspace(0,10,5)
a =
0 2.5000 5.0000 7.5000 10.0000
inital:stepsize:final
>> m = 3:8, r = 0:0.25:1, s=1:-1
m =
3 4 5 6 7 8
r =
0 0.2500 0.5000 0.7500 1.0000
s =
Empty matrix: 1-by-0
Concatenation of vectors
>> r1 = [2 4];
>> r2 = [3 6];
>> M = [r1; r2];
randn(m,n)
m × n matrix of normally distributed random numbers (mean 0, std. dev. 1))
>> M = randn(2,3)
M = -0.4336 3.5784 -1.3499
0.3426 2.7694 3.0349
repmat
>> X = [1 2;3 4];
>> Y = repmat(X,2,3)
Y = 1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
vertcat
>> v1 = [2 3 4]; v2 = [1 2 3];
>> X = vertcat(v1,v2)
X = 2 3 4
1 2 3
horzcat
>> v1 = [2; 3; 4]; v2 = [1; 2; 3];
>> X = horzcat(v1,v2)
X = 2 1
3 2
4 3
reshape()
>> x2 = reshape(y,2,4);
>> M = reshape(linspace(11,18,8),[2,2,2])
M(:,:,1) =
11 13
12 14
M(:,:,2) =
15 17
16 18
3 Operators
4 Sparse Matrices
Exponentiation (∧ )
>> (3+2*j)^2
The transpose operators turns a column vector into a row vector and vice
versa
The ′ gives the Hermitian-transpose, i.e., transposes and conjugates all
complex numbers
For vectors of real numbers .′ and ′ give same result
>> a = [ 1;5; 3i+2]
>> a’
ans =
1.0000 5.0000 2.0000 - 3.0000i
>> transpose(a)
ans =
1.0000 5.0000 2.0000 + 3.0000i
>> a.’
ans =
1.0000 5.0000 2.0000 + 3.0000i
Construct a matrix R
>> R = rand(5)
R =
0.8147 0.0975 0.1576 0.1419 0.6557
0.9058 0.2785 0.9706 0.4218 0.0357
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787
Test for some logical cases
>> R(R<0.15)’
ans =
0.1270 0.0975 0.1419 0.0357
>> isequal(R(R<0.15), R(find(R<0.15)))
ans =
1
find returns indices of nonzero values. It can simplify code and help avoid
loops
basic syntax: index = find(condition)
>> x = rand(1,10)
x =
Columns 1 through 5
0.4505 0.0838 0.2290 0.9133 0.1524
Columns 6 through 10
0.8258 0.5383 0.9961 0.0782 0.4427
>> inds = find(x>0.4 & x<0.7)
inds =
1 7 10
>> x(inds)
ans =
0.4505 0.5383 0.4427
H. Yücel (METU) MATLAB Lecture I 36 / 46
1 Introduction to MATLAB
3 Operators
4 Sparse Matrices
M = 1000;
A = diag(ones(M-1,1),-1) + diag(-2*ones(M,1),0) + diag(ones(M-1,1),1);
s = whos(’A’);
by = s.bytes;
>> by = 8000000 bytes
H. Yücel (METU) MATLAB Lecture I 39 / 46
Sparse Matrices
Triplet Format in MATLAB stores values and their corresponding row and
column values.
row = [1 2 3 1 5 4 1 5];
2 0 2 0 7
col = [1 1 2 3 3 4 5 5];
8 0 0 0 0
val = [2 8 9 2 4 5 7 3];
S =
0 9 0 0 0
S = sparse(row,col,val);
0 0 0 5 0
0 0 4 0 3
M = spones(S);
I = speye(m,n);
B = spdiags(S);
B = spdiags(S,d);
S = spdiags(B,d,S);
Create m × n sparse matrix from the columns of B and place them along the
diagonals specified by d
S = spdiags(B,d,m,n)
M = 1000;
S = spdiags([ones(M,1) -2*ones(M,1) ones(M,1)], [-1 0 1] , M, M);
s = whos(’S’);
by = s.bytes;
>> by = 55976 bytes
H. Yücel (METU) MATLAB Lecture I 43 / 46
full() converts a sparse matrix to a dense matrix
A = full(S)
spy() plots sparsity structure of a matrix.
spy(S)
10
20
30
40
50
60
70
80
90
100
0 10 20 30 40 50 60 70 80 90 100
nz = 298
Accessing the row and column indexes i, j and changing previous value
S(i, j) = c is required
When an element S(i, j) is requested, a search trough row and column values
is needed