Matrices in Matlab Vlachopoulos Georgios Lecturer of Computer Science and Informatics Technological Institute of Patras, Department of Optometry, Branch of Egion Lecturer of Biostatistics Technological.

Download Report

Transcript Matrices in Matlab Vlachopoulos Georgios Lecturer of Computer Science and Informatics Technological Institute of Patras, Department of Optometry, Branch of Egion Lecturer of Biostatistics Technological.

Matrices in Matlab
Vlachopoulos Georgios
Lecturer of Computer Science and Informatics
Technological Institute of Patras, Department of Optometry, Branch of Egion
Lecturer of Biostatistics
Technological Institute of Patras, Department of Physiotherapy, Branch of Egion

Matlab  (Matrix Laboratory)
◦ A powerful tool to handle Matrices
Vlachopoulos Georgios







A=[2,4,7]
B=[1:1:10]
C=[10:3:40]
D=[30:-3:0]
D1=[1:pi:100]
Length(D1)
D2=linspace(2,10,20)
Vlachopoulos Georgios
E=[1,2,3↲
 4,5,6]
 F=[1,2,3;4,5,6]
G=[1;2;3]
H=[1,2,3;
4,5]

Vlachopoulos Georgios
X=2;
H=[x,sin(pi/4), 3,2*x;
sqrt(5), x^2,log(x),4]
H1=[x,sin(pi/4), 3,2*x;
sqrt(5), x^2,log(x),4;
linspace(1,2,4)]
Vlachopoulos Georgios
Special functions
zeros(2,4)
zeros(2,2)
zeros(2)
ones(2,4)
ones(2,2)
ones(2)
eye(2,2)
eye(2)
eye(2,4)

Vlachopoulos Georgios
Special functions
rand (2,4)
rand(2,2)
rand(2)
magic(3)
hilb(3)

Vlachopoulos Georgios











+
*
/
\
.*
./
.\
^ (base and exp)
inv
size
Vlachopoulos Georgios

Inner Product
◦ dot(array1,array2)

Cross Product
◦ cross(array1,array2)
Vlachopoulos Georgios
Every polynomial corresponds to an array
with elements the coefficients of the
polynomial
Example
f1(x)=x2-5x+6f1=[1,-5,6]
f2(x)=x3-5x+6f2=[1,0,-5,6]

Vlachopoulos Georgios

Add polynomials
◦ array1+array2
◦ If we have different order polynomials we create equal
sizes arrays adding zeros on missing coefficients

Add polynomials
◦ array1-array2
◦ If we have different order polynomials we create equal
sizes arrays adding zeros on missing coefficients

Multiply polynomials
◦ conv(array1,array2)

Divide polynomials
◦ deconv(array1,array2)
Vlachopoulos Georgios




Roots of a polynomial
roots(array)
Polynomial with roots the elements of the
array
poly(array)
First order derivative of the Polynomial
polyder(array)
Value of the Polynomial p for x=a
polyval(p,a)
Vlachopoulos Georgios

Examples
k1=root(f1)
k2=root(f2)
poly(k1)
kder=polyder(f2)
polyval(s2,5)
Vlachopoulos Georgios



A∪Bunion(array1,array2)
A∩B intersect(array1,array2)
A∼B setdiff(array1,array2)
Example
◦
◦
◦
◦
◦
◦
a=1:6
b=0:2:10
c=union(a,b)
d=intersect(a,b)
e1=setdiff(a,b)
e2=setdiff(b,a)
Vlachopoulos Georgios
Unique Elements  unique(array)
 Elements of A that are members of B 
ismember(array1,array2)
Example

◦
◦
◦
◦
f1=ismember(a,b)
f2=ismember(b,a)
g=[1,1,2,2,3,3]
h=unique(g)
Vlachopoulos Georgios

Arrays
◦ Sum of array elements  sum(array)
◦ Product of array elements  prod(array)
◦ Cumulative sum of an array elements
cumsum(array)
◦ Cumulative prod of an array elements
cumprod(array)
Vlachopoulos Georgios

Matrices
◦ Sum of elements of each matrix column
 sum(matrix)
or
 sum(matrix,1)
◦ Sum of elements of each matrix row
 sum(matrix,2)
Overall sum????
Vlachopoulos Georgios

Matrices
◦ Product of elements of each matrix column
 prod(matrix)
or
 prod(matrix,1)
◦ Product of elements of each matrix row
 prod(matrix,2)
Overall product????
Vlachopoulos Georgios

Matrices
◦ Cumulative sum per column
 cumsum(matrix)
or
 cumsum (matrix,1)
◦ Cumulative sum per row
 cumsum (matrix,2)
Vlachopoulos Georgios

Matrices
◦ Cumulative sum per column
 cumprod(matrix)
or
 cumprod (matrix,1)
◦ Cumulative sum per row
 cumprod(matrix,2)
Vlachopoulos Georgios
Matrix element A(i,j)
Example:
A=[1,2,3;4,5,6]
A(2,1)↲
A(2,1)=4

Vlachopoulos Georgios
Example:
A=[1,2,3;4,5,6;3,2,1]
B=A(1:2,2,3)
y=A(:,1)
Z=A(1,:)
W=A([2,3],[1,3])
A(:)
Vlachopoulos Georgios
Delete elements
Example

◦
◦
◦
◦
◦
◦
Clear all;
A=magic(5)
A(2,: )=[] % delete second row
A(:[1,4])=[] % delete columns 1 and 4
A=magic(5)
A(1:3,:)=[] % delete rows 1 to 3
Vlachopoulos Georgios
Replace Elements
Example

◦
◦
◦
◦
◦
Clear all;
A=magic(5)
A(2,3 )=5 % Replace Element (2,3)
A(3,:)=[12,13,14,15,16] % replace 3rd row
A([2,5]=[22,23,24,25,26; 32,33,34,35,36]
Vlachopoulos Georgios
Insert Elements
Example

◦
◦
◦
◦
Clear all;
A=magic(5)
A(6,:)=[1,2,3,4,5,6]
A(9,:)=[11,12,13,14,15,16]
Vlachopoulos Georgios