# Vektoren und Matrizen

(annsvecmtx.sci)

``` //Functions to compute with vectors and matrices
//Author: Gerd Doeben-Henisch
//Version: 7.Jan 2009
//Some common constants

//***************************************************
//Function to compute the sum of a vector v and the i-th row of a matrix M

//Test whether the length of v is equal to the columns of M

l=length(v);
[r,c]=size(M);
if l <> c then error('SIZE of VECTOR DOES NOT MATCH MATRIX'),
else
for j=1:c, M(i,j) = M(i,j)+v(j),
end
end
endfunction

//***************************************************
//Function to compute the difference of a vector v and the i-th row of a matrix M

function[M]=vecSubMi(M,i,v)

//Test whether the length of v is equal to the columns of M

l=length(v);
[r,c]=size(M);
if l <> c then error('SIZE of VECTOR DOES NOT MATCH MATRIX'),
else
for j=1:c, M(i,j) = M(i,j)-v(j),
end
end
endfunction

//***************************************************
//Function to compute the product of two vectors v,w

function[u]=vecprod(v,w)

lv=length(v);
lw=length(w);

u=0

if lv <> lw then error('DIFFERENT LENGTH OF VECTORS');

else  for i=1:lv, u=u + (v(i)*w(i));
end
end

endfunction

//***************************************************
//Function to compute the projection of a vector v onto a line with angle theta
//angle in degree

function[y]=vecproj(v,t)

y=norm(v)*cos(t)

endfunction

//***************************************************
//Function to compute the projection of a vector v onto a line with line w

function[y]=vecprojL(v,w)

y=vecprod(v,w)/norm(w)

endfunction

//*************************************************************
//Function to compute the angle of two vectors v,w in 360 degree
//Vectors v,w are column or row vectors

function[y]=cosvec(v,w)

y=vecprod(v,w)/(norm(v)*norm(w))

//The result in y is the cos-value. We need the invers of this

y=acos(y)

//The inverse is given in radiants. We want the conversion to degrees

endfunction
//End of function cosvect ******************************************

//*******************************************************
//Function to multiply a vector v with a quadratic matrix M generating a vector u

function[u]=vmprod(v,M)

[r,c]=size(M)
lv=length(v)
u=zeros(1,lv)

if (r <> lv)  then error('sizes of vector and matrix do not match')
else

for i=1:c  //Following the columns of matrix
for j=1:r  //Working through all elements of v and rows of M
u(j)=u(j) + (v(i) *M(i,j))
end
end
end

endfunction

//*******************************************************
//Function to multiply a vector v with a selectable matrix M generating a vector u

function[u]=vmprod2(M,v)

[r,c]=size(M)
lv=length(v)
u=zeros(1,lv)

if (r <> lv) then error('sizes of vector and matrix do not match')
else

for j=1:r  //Following the rows of matrix
for i=1:c  //Working through all elements of v and columns of M
u(j)=u(j) + (v(i) *M(j,i))
end
end
end

endfunction

//****************************************************
//Funcion to generate a m x n - Matrix with the scalar c

function[C] = matrixC(r,c,a)

C=[] //New Matrix
for i=1:c; for j=1:r; C(i,j)=a; end;  end //Generate new matrix with scalar a
C

endfunction

//****************************************************
//Funcion to generate a m x n - Matrix with the scalar c out of a given matrix M

function[C] = matrixCC(M,a)

C=[] //New Matrix
[c,r]=size(M) //Get dimension of given matrix M by columns and rows
for i=1:r, for j=1:c, C(i,j)=a, end, end //Generate new matrix with scalar a

endfunction
```

Gerd Doeben-Henisch 2013-01-17