690 likes | 1.2k Views
MATLAB Vectors & Matrices. Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun. Introduction. In MATLAB, matrix is chosen as a basic data element Vector: Matrix of 1xn or nx1 is know as vector. row vector column vector >>p=[1 2 3]; % data assignment for row vector
E N D
MATLABVectors & Matrices Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun
Introduction • In MATLAB, matrix is chosen as a basic data element • Vector: • Matrix of 1xn or nx1 is know as vector. • row vector column vector • >>p=[1 2 3]; % data assignment for row vector • or • >>p=[1,2,3] ; • >>q=[1;2;3]; %data assignment for column vector • or • >>q=[1 • 2 • 3];
Working with vectors • Scalar: • Matrix of 1x1 • >>r=3; %data assignment for scalar • In MATLAB it is possible to work with the complete matrix simultaneously. • Vector Product • >>x=[3;4;5]; %column vector of 3x1 • >>y=[1 2 0]; %row vector of 1x3 • >>z=y*x Z=11 %scalor >>z=x*y %matrix of 3x3 z= 3 6 0 4 8 0 5 10 0
Working with vectors Vector transpose >>xt=x’ % transpose of x >>xt= 3 4 5 >>yt=y’ yt= 1 2 0 Creating Evenly spaced row vector >>a=1:2:11 %staring from 1 with an increment of 2 and upto 11 a = 1 3 5 7 9 11
Working with vectors • Exercise • >>t=12.5:-2.5:0 • >>t=11:1:5 • >>t=1:20 • Linspace command • >>a=Linspace(0,10,5) %linspace(x1,x2,n) , n equally spaced elements starting from x1 end with x2 • >>a=logspace(0,4,3) • %logspace(a,b,n), logarithmically spaced vector of length n in the interval 10a to 10b
Working with vectors • Exercise • >>x=[1 4 11 100]; • >>y=[14; 200; -100]; • >>z=[1.4 10.7 -1.1 20.9]; • Try this • sum(x) %sum of all elements of row or column vector • mean(x) %ave of all elements of row or column vector • Max(x) • Min(x) • Prod(x) %product of all elements of row or column vector • Sign(x) %return +1 if sign of element is +ve 0 if element is zero -1 if sign of element is -ve
Find(x) %returns the linear indices corresponding to non-zero entries of the array x >>a=find(x) a= 1 2 3 4 %1st , 2nd, 3rd, & 4th, elements are non zeros >>a=find(y>24) a= 2 %2nd element of y has value > 24 • Fix (z) %rounds the elements of a vector z to nearest integers towards zero. • Floor(z) %rounds the elements of a vector z to nearest integers towards –infinity • Ceil (z) %rounds the elements of a vector z to nearest intergers towards +infinity • Round(x) %rounds the elements to nearest integer
Sort(x, ‘mode’) %for sorting, mode=ascend or descend, default is ascend. • >>x1=[5 -3 10 -10]; • >>a=sort(x1) • >>a= -10 -3 5 10 • >>b=sort(x1, ‘descend’) • >>b = 10 5 -3 10 • Mod(x,y) %Modulus after division • Rem(x,y) %Remainder after division
Working with Matrices • Entering data in matrices • >>A=[1 10 20; 2 5 6;7 8 9] • >>B=[1+2i 3i; 4+4i 5] % i or j • >>C=[1 -2; sqrt(3) exp(1)] • Line continuation: sometimes it is not possible to type data input on the same line • >>A=[1 10 20; 2 5 6;7 8 9] %semi column to separate rows • >>A=[1 10 20 %Enter key or carriage return 2 5 6 7 8 9] • >>A=[1, 10, 20; 2, 5,… %ellipsis(3 dots …) method 6;7, 8, 9]
Working with Matrices • Sub-matrices • >>B=A(1:2,2:3) %row 1 to 2 & column 2 to 3 • >>B= 10 20 5 6 • >>B= A(:, 2:3) %all row & column 2 to 3 • >>B=A(:, end) %end=> last column (or row) • Size of matrix • >>[m, n]=size(A)
Multidimensional Arrays\Matrices • Creating multidimensional arrays: Consider a book, line no & column no represents two dimensions and third dimension is page no. Three methods • 1. Extending matrix of lower dimension • 2. Using MATLAB function • 3. Using ‘cat’ function
Multidimensional Arrays\Matrices • 1 Extending matrix dimension • >>A=[1 2 3; 5 4 3; 1 3 6]; • >>B=[2 4 6; 1 3 6; 3 6 9]; • >>A(:,:,2)=B • A(:,:,1) = 1 2 3 5 4 3 1 3 6 A(:,:,2) = 2 4 6 1 3 6 3 6 9
Multidimensional Arrays\Matrices • 2. Using MATLAB functions • >>B=randn(4, 3, 2) %random no multidimensional matrix B(:,:,1) = %similarly ‘ones’ & ‘zeros’ function 0.5377 0.3188 3.5784 1.8339 -1.3077 2.7694 -2.2588 -0.4336 -1.3499 0.8622 0.3426 3.0349 B(:,:,2) = 0.7254 -0.1241 0.6715 -0.0631 1.4897 -1.2075 0.7147 1.4090 0.7172 -0.2050 1.4172 1.6302
Multidimensional Arrays\Matrices • 3. Using ‘cat’ function: concatenates a list of array • >>A1=[1 3; 6 9]; • >>B1=[3 3; 9 9]; • >>B=cat(2, A1, B1) B = 1 3 3 3 6 9 9 9 • Working with multidimensional arrays: Most of the concepts are similar to two dimensional arrays
Matrix Manipulations • Reshaping matrix into a vector • >>A=[1 10 20; 2 5 6; 7 8 9] • >>B=A(:) %converts to column matrix B = 1 2 7 10 5 8 20 6 9
Matrix Manipulations • Reshaping a matrix into different sized matrix >>A=[1 2 3 4; 5 6 7 8; 9 10 11 12] % A is 3x4 matrix >>B=reshape(A, 6,2) % reshaped matrix B is 6x2 B = 1 3 5 7 9 11 2 4 6 8 10 12 Note: total no of elements 3x4=6x2=12 must be same
Matrix Manipulations • Expanding matrix size >>C(2,2)=10 %D is 2x2 with last element D(2,2)=10 C= 0 0 0 10 >>D(2,1:2)=[3 4] %D is 2x2 with element D(2,1)=3 & D(2,2)=4 D= 0 0 • 4 >>A=[6 7; 8 9]; %A is 2x2 matrix >>A(2,3)=15 %Now A is changed to 2x3 matrix A = 6 7 0 8 9 15
Matrix Manipulations • Appending/Deleting a row/column to a matrix >>A=[6 7; 8 9]; >>x=[1; 2]; %Column vector >>y=[3 4]; %Row vector >>B=[A x] %Appending a column ‘x’ B = 6 7 1 8 9 2 >>C=[A; y] %Appending a row ‘y’ C = 6 7 8 9 3 4
Matrix Manipulations >>C(2,:)=[ ] %delete 2nd row of matrix C C = 6 7 3 4 >>B(:,1:2)=[ ] %delete 1st to 2nd column of matrix B B = 1 2 Note: Deletion of single element is not allowed, we can replace it.
Matrix Manipulations Concatenation of matrices >>A=[1 2; 3 4]; >>B=[A A+12; A+24 A+10] B = 1 2 13 14 3 4 15 16 25 26 11 12 27 28 13 14
Generation of special Matrices Try this >>A=zeros(2,3) >>B=ones(3,4) >>C=eye(3,2) %1s in main diagonal rest elements will be zero >>D=rand(3) %3x3 matrix with random no b/w 0 to1 >>E=rands(3) %3x3 matrix with random no b/w --1 to1 >>V=vander(v) %Vandermode matrix, V whose columns are powers of the vector v. Let v=[1 2 3]. Here 3 elements => V is 3x3 Note: zeros(3,3) may be written as zeros(3) and so the others also.
Generation of special Matrices >>d=[2 3 4 5]; %Note ‘d’ may be row/column vector >>A=diag(d) %diagonal of A (4x4) will be 2,3,4,5 and rest ‘0’ A =2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 >>B=diag(d,1) %1st upper diagonal elements are vector d A = 0 2 0 0 0 0 0 3 0 0 0 0 0 4 0 0 0 0 0 5 0 0 0 0 0 >>C=diag(d,-1) %1st lower diagonal elements are vector d
Generation of special Matrices >>x=[1 2 3; 4 5 6;7 8 9]; %Note x is a 3x3 matrix now >>A=diag(x) %will give you the diagonal elements A = 1 5 9 Note: diag(x,1)=>1st upper diagonal elements diag(x,-1)=>1st lower diagonal elements
Some useful commands for matrices • >>A=[1 2; 0 4]; • >>det(A) %determinant of A • >>rank(A) %rank of A • >>trace(A) %sum of diagonal elements • >>inv(A) %inverse of A • >>norm(A) %Euclidean norm of A • >>A’ %transpose of A • >>x=A\b %left division • >>poly(A) %coefficients of characteristic equation i.e (sI-A) • >>eig(A) %gives eign values of A • >>[v,x]=eig(A) %returns v=eign vector & x=eign values • >>B=orth(A) %B will be orthogonal to A i.e. B’=B-1 • >>Find(A) %returns indices of non-zeros elements • >>sort(A) %sort each column in ascending order
Matrix and Array Operation • Arithmetic operation on Matrix >>A=[5 10; 15 20]; >>B=[2 4; 6 8]; Try these >>C=A+B %or C=plus(A,B) addition >>D=A-B %or D=minus(A,B) Subtraction >>E=A*B %Multiplication >>F=A^2 %Power >>G=A/B %Right Division >>H=A\B %Left Division Example: Solve A.x=B where A=[2 4; 5 2] & B=[6;15] Sol: x=A-1B=A\B
Matrix and Array Operation • Arithmetic operation on Arrays (Element by Element Operation) >>A=[5 10; 15 20]; >>B=[2 4; 6 8]; Try these Note: 1. Addition and subtraction are same 2. No of Rows and Columns of two matrices must be same >>E=A.*B % Element by Element Multiplication >>F=A.^2 % Element by Element Power >>G=A./B % Element by Element Right Division >>H=A.\B % Element by Element Left Division
Rational Operators • < Less than • <= Less than equal to • > Greater than • >= Greater than equal to • == Equal to • ~= Not equal to • Note: true =1; false =0, try: >>6>5 on MATLAB command window
Logical Operators • & Logical AND • | Logical OR • ~ Logical NOT, complements every element of an array • xor Logical exclusive-OR • Try these >>x=[2 3 4]; >>y=[2 5 1]; >>x&y >>x|y >>m=~x % complements every element of an array >>m=xor(x,y) • Note: true =1; false =0, try >>6>5 on matlab command window
Function with array inputs • If input to a function is an array then function is calculated element-by-element basis. • Try this • >>x=[0, pi/2,pi]; • >>y=sin(x) • y = 0 1.0000 0.0000 • >>z=cos(x) • z = 1.0000 0.0000 -1.0000
Structure Arrays • Structure: • Collection of different kinds of data(text, number, numeric array etc), unlike array which contain elements of same data type. • Again this is 1x1 structure array • Try this >>student.name=‘Kalpana Rawat’ >>student.rollno=44 >>student.marks=[45 33 15 18 0] >>student student = name: 'Kalpana Rawat' marks: [45 33 15 18 0] rollno: 44 Note: Here student is structure name & name, rollno, marks are field name
Structure Arrays Student is a 1x1 structure array having 3 fields. To increase the size of structure array define the second structure element of the array as >>student(2).name=‘Kuldeep Rawat’; >>student(2).rollno=57; >>student(2).marks=[4 13 35 36 9]; >>student student = 1x2 struct array with fields: name marks rollno Note: Here structure student will show you only field names not filed values
Structure Arrays • Struct function • A function struct can be used to define a structure array. Syntax is Student=struct(‘filed1’,vaule1, ‘filed2’, value2,….) • Previous structure example can be rewritten as >> student=struct('name','Kalpana Rawat','rollno',44,'marks',[44 34 67 19 9]) >> student(2)=struct('name',‘Mallika Rawat','rollno',45,'marks',[22 14 27 29 0]) >> student 1x2 struct array with fields: name rollno marks Note: Nesting of structure is also possible i.e filed may be another structure
Structure Arrays • Obtaining data from structures >>first_student_name=student(1).name first_student_name = Kalpana Rawat >>first_student_rollno=student(1).rollno first_student_rollno = 44 >>first_student_Marks=student(1).marks first_student_Marks = 44 34 67 19 9 • Try these >>Second_student_name=student(2).name >>Second_student_rollno=student(2).rollno >>Second_student_Marks=student(2).marks
Cell Arrays Cell Arrays: Array of Cells >> sample=cell(2,2); %sample is a 2x2 cell array Entering values in cell arrays >> sample(1,1)={[54 37 59; 18 69 59; 72 27 49]}; >>ample(1,2)={'Mallika Tiwari'}; >>sample(2,1)={[2i,1-7i,-6]}; >>sample(2,2)={['abcd', 'efgh', 'ijkl']}; To display cell array sample in condensed form, type >>sample sample = [3x3 double] 'Mallika Tiwari' [1x3 double] 'abcdefghijkl'
Cell Arrays To display the full cell contents use celldisp function >> celldisp(sample) sample{1,1} = 54 37 59 18 69 59 72 27 49 sample{2,1} = 0 + 2.0000i 1.0000 - 7.0000i -6.0000 sample{1,2} = Mallika Tiwari sample{2,2} = abcdefghijkl
Cell Arrays For graphical display use cellplot >> cellplot(sample)
Some useful commands of structure & cell • Cell2struct, syntax sample_struct=cell2struct(sample, fields, dimen) • Num2cell , syntax c_array=num2cell(number) • Struct2cell , syntax c_array=struct2cell(sample-struct)