• Ei tuloksia

To extract rows and columns from a matrix: Given the matrixA above, if you want the first column ofA, or a submatrix containing only the first two rows of A, that is

N/A
N/A
Info
Lataa
Protected

Academic year: 2022

Jaa "To extract rows and columns from a matrix: Given the matrixA above, if you want the first column ofA, or a submatrix containing only the first two rows of A, that is"

Copied!
3
0
0

Kokoteksti

(1)

Computational Methods in Inverse Problems, Mat–1.3626

Fall 2007 Erkki Somersalo/Knarik Tunyan

Matlab Primer – How to Get Started

Matlab is a high level programming language particularly suitable for numerical linear algebra. The basic object in Matlab are matrices and vectors. To create arrays

x=

 2

−3 1

, A=

1 0 4

−1 2 1

−2 6 0

,

the comands are

x = [2;-3;1];

A = [1,0,4;-1,2,1;-2,6,0];

If you leave the semicolon at the end of the line out, Matlab echoes the array, which may be a good way to check that you did not do errors.

Matrix products and transposes: To calculate y=Ax, B=AT, type

y = A*x;

B = A’;

To extract rows and columns from a matrix: Given the matrixA above, if you want the first column ofA, or a submatrix containing only the first two rows of A, that is

 1

−1

−2

,

1 0 4

−1 2 1

,

you may type simply

A(:,1) A(1:2,:)

More generally, if C is a n×m matrix and you want to create a submatrix containing only j rows whose numbers you have collected to an index vector I= [k1, k2, . . . , kj], write

C(I,:)

1

(2)

Conversely, if you want to merge two column vectors of same length into an array, try this:

a = [4;0;-1];

b = [1;2;0];

A = [a,b];

The result is a 3×2 arrayaandbas its columns.

Plot command in Matlab are versatile and of great help, e.g., when debugging a code. Suppose you want to plot the functiont7→tcos(πt), 0≤t≤1. What you have to do is to form a vector discretizing the interval bynsampling points and another vector containing the function evaluations at those points:

n = 100;

t = linspace(0,1,n);

y = t.*cos(pi*t);

plot(t,y)

Notice the difference between the matrix product “*” and the pointwise multi- plication of two vectors of equal size, “*.”.

Solving linear systems: LetAbe a square matrix of sizen×nandba column vector of sizen. To find then–vector xsatisfying

Ax=b, the Matlab command is

x = A\b;

If the matrix is not invertible or very ill-conditioned, Matlab lets you know (Try!). If you have a non-square matrix A ∈ Rm×n and a column vectorb ∈ Rm, the command above returns the Least Squares Minimum Norm (LSQMN) solution, which can be written in terms of the pseudo-inverse ofA,

x=Ab∈Rn.

The Singular Value Decomposition (SVD) of a matrix, A=U DVT,

is obtained by the command line [U,D,V] = svd(A);

Suppose you want to plot the singular values in a logarithmic scale. You may try the commands

2

(3)

d = diag(D);

plot(log(d));

giving you a curve plot. If you want to plot the singular values as red dots, you may try

plot(log(d),’r.’,’MarkerSize’,15)

An automatic scaling to logarithmic scale, with slightly smaller blue dots, you get by typing

semilogy(d,’b.’,’MarkerSize’,10)

More Matlab appears on these pages as the course goes on.

3

Viittaukset

LIITTYVÄT TIEDOSTOT

Kulttuurinen musiikintutkimus ja äänentutkimus ovat kritisoineet tätä ajattelutapaa, mutta myös näissä tieteenperinteissä kuunteleminen on ymmärretty usein dualistisesti

Others may be explicable in terms of more general, not specifically linguistic, principles of cognition (Deane I99I,1992). The assumption ofthe autonomy of syntax

The new European Border and Coast Guard com- prises the European Border and Coast Guard Agency, namely Frontex, and all the national border control authorities in the member

The US and the European Union feature in multiple roles. Both are identified as responsible for “creating a chronic seat of instability in Eu- rope and in the immediate vicinity

At this point in time, when WHO was not ready to declare the current situation a Public Health Emergency of In- ternational Concern,12 the European Centre for Disease Prevention

Indeed, while strongly criticized by human rights organizations, the refugee deal with Turkey is seen by member states as one of the EU’s main foreign poli- cy achievements of

However, the pros- pect of endless violence and civilian sufering with an inept and corrupt Kabul government prolonging the futile fight with external support could have been

Two open Survo puzzles A and B are defined essentially different if the solution of A cannot be transformed into the solution of B by interchanging rows and columns or by