User talk:Eml4500.f08.qwiki.nobles

eml4500.f08.qwiki.nobles/Lecture 1 >> % Matrix building functions >> % A few matrix building functions include >> % eye : identity matrix >> % zeros: matrix of zeros >> % ones : matrix of ones >> % diag : create or extract diagonals >> % triu : upper triangular part of a matrix >> % tril : lower triangular part of a matrix >> % rand : randomly generated matrix >> % hilb : Hilbert matrix >> % magic : magic square >> % toeplitz : see help >> % zeros(m,n) produces an m-by-n matrix of zeros and zeros(n) produces an n-by-n one >> zeros(3)

ans =

0    0     0     0     0     0     0     0     0

>> zeros(2,3)

ans =

0    0     0     0     0     0

>> A = ans; >> zeros(size(A))

ans =

0    0     0     0     0     0

>> % diag(x) produces a matrix with the vector x down the diagonal; >> % if A is a square matrix, then diag(A) is a vector consisting of the diagonal of A >> x = [1 2 3 4]; >> diag(x)

ans =

1    0     0     0     0     2     0     0     0     0     3     0     0     0     0     4

>> A = [1 2; 3 4]

A =

1    2     3     4

>> diag(A)

ans =

1    4

>> diag(diag(A))

ans =

1    0     0     4

>> % matrices can be built from blocks; if A is a 3-by-3 matrix, then >> A = ones(3,3); >> B = [A, zeros(3,2); zeros(2,3), eye(2)]

B =

1    1     1     0     0     1     1     1     0     0     1     1     1     0     0     0     0     0     1     0     0     0     0     0     1

>> % Flow Controls >> % for : executes a block of code a specified number of times >> n=4; >> x=[]; for i=1:n, x=[x,i^2],end

x =

1

x =

1    4

x =

1    4     9

x =

1    4     9    16

>> m=3; >> for i=1:m, for j=1:n, H(i,j)=1/(i+j-1); end, end, H

H =

1.0000   0.5000    0.3333    0.2500    0.5000    0.3333    0.2500    0.2000    0.3333    0.2500    0.2000    0.1667

>> % the semicolon in the above code suppresses printing of H >> % while the last H displays the final result, a hilbert matrix >> % while loops allow their statements to be repeatedly executed as long as >> % the relation remains true >> a=5; >> n=0; >> while 2^n < a, n=n+1; end, n

n =

3

>> % the "if" statement executes the procedures inside the loop as long as the relation is true >> if n<0, parity = 0; elseif rem(n,2) == 0, parity = 2; else, parity = 1; end >> parity

parity =

1

>> % Here are the relation operators in MATLAB >> % < less than >> % > greater than >> % <= less than or equal >> % >= greater than or equal >> % == equal >> % ~= not equal >> % note: "=" is used for assignment while "==" is used for relation >> % relations can be connected or quantified by logical operators >> % "&" and, "|" or, "~" not >> % when applied to scalars, a relation is actually the scalar 1 for true >> % or 0 for false >> 3<5

ans =

1

>> 3>5

ans =

0

>> 3==5

ans =

0

>> 3==3

ans =

1

>> a = rand(5)

a =

0.9355   0.3529    0.1987    0.7468    0.8462    0.9169    0.8132    0.6038    0.4451    0.5252    0.4103    0.0099    0.2722    0.9318    0.2026    0.8936    0.1389    0.1988    0.4660    0.6721    0.0579    0.2028    0.0153    0.4186    0.8381

>> b = triu(a)

b =

0.9355   0.3529    0.1987    0.7468    0.8462         0    0.8132    0.6038    0.4451    0.5252         0         0    0.2722    0.9318    0.2026         0         0         0    0.4660    0.6721         0         0         0         0    0.8381

>> a==b

ans =

1    1     1     1     1     0     1     1     1     1     0     0     1     1     1     0     0     0     1     1     0     0     0     0     1

>> % a relation between matrices is interpreted by "while" and "if" to be true if each >> % entry of the relation matrix is nonzero. for example if A and B are equal matrices, then >> % if A==B, "statement"; end >> % certain functions operate on scalars, but operate element-wise when applied to a matrix >> % common functions include: >> % sin   asin    exp    abs    round >> % cos   acos    log    sqrt   floor >> % tan   atan    rem    sign   ceil >> % see help files for examples of these