vlachopoulos georgios lecturer of computer science and informatics technological institute of...
TRANSCRIPT
Research Methodology
Programming in Matlab
Vlachopoulos GeorgiosLecturer of Computer Science and Informatics
Technological Institute of Patras, Department of Optometry, Branch of EgionLecturer of Biostatistics
Technological Institute of Patras, Department of Physiotherapy, Branch of Egion
Matrices in Matlab
Vlachopoulos Georgios
Arrays and Matrices Matlab (Matrix Laboratory)
◦ A powerful tool to handle Matrices
Vlachopoulos Georgios
Define an array 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]
Define a Matrix
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)]
Define a Matrix
Vlachopoulos Georgios
Special functionszeros(2,4)zeros(2,2)zeros(2)ones(2,4)ones(2,2)ones(2)eye(2,2)eye(2)eye(2,4)
Define a Matrix
Vlachopoulos Georgios
Special functionsrand (2,4)rand(2,2)rand(2)magic(3)hilb(3)
Define a Matrix
Vlachopoulos Georgios
+ - * / \ .* ./ .\ ^ (base and exp) inv size
Basic Operators to Matrices
Vlachopoulos Georgios
Inner Product◦ dot(array1,array2)
Cross Product◦ cross(array1,array2)
Special Operators to Matrices
Vlachopoulos Georgios
Every polynomial corresponds to an array with elements the coefficients of the polynomial
Examplef1(x)=x2-5x+6f1=[1,-5,6]
f2(x)=x3-5x+6f2=[1,0,-5,6]
Polynomials as arrays
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)
Polynomials as arrays
Vlachopoulos Georgios
Roots of a polynomial roots(array)
Polynomial with roots the elements of the array poly(array)
First order derivative of the Polynomialpolyder(array)
Value of the Polynomial p for x=apolyval(p,a)
Polynomials as arrays
Vlachopoulos Georgios
Examplesk1=root(f1)k2=root(f2) poly(k1)kder=polyder(f2)polyval(s2,5)
Polynomials as arrays
Vlachopoulos Georgios
A∪Bunion(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)
Set Operations
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)
Set Operations
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)
Calculation Functions
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????
Calculation Functions
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????
Calculation Functions
Vlachopoulos Georgios
Matrices◦ Cumulative sum per column cumsum(matrix) or cumsum (matrix,1)◦ Cumulative sum per row cumsum (matrix,2)
Calculation Functions
Vlachopoulos Georgios
Matrices◦ Cumulative sum per column cumprod(matrix) or cumprod (matrix,1)◦ Cumulative sum per row cumprod(matrix,2)
Calculation Functions
Vlachopoulos Georgios
Matrix element A(i,j)Example:
A=[1,2,3;4,5,6]A(2,1)↲A(2,1)=4
Reference to Martix elements
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(:)
Reference to Martix elements
Vlachopoulos Georgios
Delete elementsExample
◦ 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
Edit Matrices
Vlachopoulos Georgios
Replace ElementsExample
◦ 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]
Edit Matrices
Vlachopoulos Georgios
Insert ElementsExample
◦ Clear all;◦ A=magic(5)◦ A(6,:)=[1,2,3,4,5,6]◦ A(9,:)=[11,12,13,14,15,16]
Edit Matrices