3 Vectors and Matrices
3 Vectors and Matrices
Vector:
Matrix of 1xn or nx1 is know as vector.
• 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= 1020
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
>>B=ones(3,4)
• 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:
Structure Arrays
I. Collection of different kinds of data(text, number, numeric array etc), unlike
array which contain elements of same data type.
II. 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
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]};
>>sample(1,2)={'Mallika Tiwari'};
>>sample(2,1)={[2i,1-7i,-6]};
>>sample(2,2)={['abcd', 'efgh', 'ijkl']};
• Num2cell , syntax
c_array=num2cell(number)
• Struct2cell , syntax
c_array=struct2cell(sample-struct)
=