Download presentation
Presentation is loading. Please wait.
1
MATLAB Vectors & Matrices
Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun
2
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=[ ]; % data assignment for row vector or >>p=[1,2,3] ; >>q=[1;2;3]; %data assignment for column vector >>q=[1 2 3];
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= 4 8 0 5 10 0
4
Working with vectors Vector transpose >>xt=x’ % transpose of x >>xt= >>yt=y’ yt= Creating Evenly spaced row vector >>a=1:2:11 %staring from 1 with an increment of 2 and upto 11 a =
5
Working with vectors Exercise Linspace command >>t=12.5:-2.5:0
>>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
6
Working with vectors Exercise >>x=[1 4 11 100];
>>y=[14; 200; -100]; >>z=[ ]; 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 if element is zero -1 if sign of element is -ve
7
%1st , 2nd, 3rd, & 4th, elements are non zeros
Find(x) %returns the linear indices corresponding to non-zero entries of the array x >>a=find(x) a= %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
8
Mod(x,y) %Modulus after division Rem(x,y) %Remainder after division
Sort(x, ‘mode’) %for sorting, mode=ascend or descend, default is ascend. >>x1=[ ]; >>a=sort(x1) >>a= >>b=sort(x1, ‘descend’) >>b = Mod(x,y) %Modulus after division Rem(x,y) %Remainder after division
9
Working with Matrices Entering data in matrices
>>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=[ ; ; ] %semi column to separate rows >>A=[ %Enter key or carriage return ] >>A=[1, 10, 20; 2, 5,… %ellipsis(3 dots …) method 6;7, 8, 9]
10
Working with Matrices Sub-matrices
>>B=A(1:2,2:3) %row 1 to 2 & column 2 to 3 >>B= 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)
11
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
12
Multidimensional Arrays\Matrices
1 Extending matrix dimension >>A=[ ; ; ]; >>B=[ ; ; ]; >>A(:,:,2)=B A(:,:,1) = A(:,:,2) =
13
Multidimensional Arrays\Matrices
2. Using MATLAB functions >>B=randn(4, 3, 2) %random no multidimensional matrix B(:,:,1) = %similarly ‘ones’ & ‘zeros’ function B(:,:,2) =
14
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 = Working with multidimensional arrays: Most of the concepts are similar to two dimensional arrays
15
Matrix Manipulations Reshaping matrix into a vector
>>B=A(:) %converts to column matrix B = 1 2 7 10 5 8 20 6 9
16
Matrix Manipulations Reshaping a matrix into different sized matrix
>>A=[ ; ; ] % A is 3x4 matrix >>B=reshape(A, 6,2) % reshaped matrix B is 6x2 B = Note: total no of elements 3x4=6x2=12 must be same
17
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= 4 >>A=[6 7; 8 9]; %A is 2x2 matrix >>A(2,3)=15 %Now A is changed to 2x3 matrix A =
18
Matrix Manipulations Appending/Deleting a row/column to a matrix
>>x=[1; 2]; %Column vector >>y=[3 4]; %Row vector >>B=[A x] %Appending a column ‘x’ B = >>C=[A; y] %Appending a row ‘y’ C =
19
Matrix Manipulations >>C(2,:)=[ ] %delete 2nd row of matrix C C = >>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.
20
Matrix Manipulations Concatenation of matrices >>A=[1 2; 3 4]; >>B=[A A+12; A+24 A+10] B =
21
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.
22
Generation of special Matrices
>>d=[ ]; %Note ‘d’ may be row/column vector >>A=diag(d) %diagonal of A (4x4) will be 2,3,4,5 and rest ‘0’ A = >>B=diag(d,1) %1st upper diagonal elements are vector d A = >>C=diag(d,-1) %1st lower diagonal elements are vector d
23
Generation of special Matrices
>>x=[ ; ; ]; %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
24
Some useful commands for matrices
>>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
25
Matrix and Array Operation
Arithmetic operation on Matrix >>A=[5 10; ]; >>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
26
Matrix and Array Operation
Arithmetic operation on Arrays (Element by Element Operation) >>A=[5 10; ]; >>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
27
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
28
Logical Operators & Logical AND | Logical OR
~ Logical NOT, complements every element of an array xor Logical exclusive-OR Try these >>x=[ ]; >>y=[ ]; >>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
29
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 = >>z=cos(x) z =
30
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=[ ] >>student student = name: 'Kalpana Rawat' marks: [ ] rollno: 44 Note: Here student is structure name & name, rollno, marks are field name
31
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=[ ]; >>student student = 1x2 struct array with fields: name marks rollno Note: Here structure student will show you only field names not filed values
32
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',[ ]) >> student(2)=struct('name',‘Mallika Rawat','rollno',45,'marks',[ ]) >> student 1x2 struct array with fields: name rollno marks Note: Nesting of structure is also possible i.e filed may be another structure
33
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 = Try these >>Second_student_name=student(2).name >>Second_student_rollno=student(2).rollno >>Second_student_Marks=student(2).marks
34
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)={[ ; ; ]}; >>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'
35
Cell Arrays To display the full cell contents use celldisp function
>> celldisp(sample) sample{1,1} = sample{2,1} = i i sample{1,2} = Mallika Tiwari sample{2,2} = abcdefghijkl
36
Cell Arrays For graphical display use cellplot >> cellplot(sample)
37
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)
38
= Hello
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.