Lecture 2
Lecture 2
Lecture 2
Programming in MATLAB
Lecture-2
Matrix operations
1 2 3
4 5 6
7 8 10
Matrix operations
a + 10 sin(a)
ans = ans =
Transpose
a = [1 2 3; 4 5 6; 7 8 10]
a'
a= a=
1 2 3 1 4 7
4 5 6 2 5 8
7 8 10 3 6 10
Element wise Matrix Multiplication
p = a.*a
Where,
p= a=
1 2 3
1 4 9 4 5 6
7 8 10
16 25 36
49 64 100
a.^3 a./3
ans = ans =
p = [a,a]
Where,
a=
p=
1 2 3
4 5 6
1 2 3 1 2 3 7 8 10
4 5 6 4 5 6
Concatenating arrays next to one another using commas is
7 8 10 7 8 10 called horizontal concatenation. Each array must have
the same number of rows.
Concatenation
When the arrays have the same number of columns, you can
concatenate vertically using semicolons.
p = [a;a]
p=
1 2 3
4 5 6
7 8 10
1 2 3
4 5 6
7 8 10
Complex numbers
sqrt(-1)
ans =
0.0000 + 1.0000i
To represent the imaginary part of complex
numbers, use either i or j .
c=
c = [3+4i, 4+3j; -i, 10j] 3.0000 + 4.0000i 4.0000 + 3.0000i
0.0000 - 1.0000i 0.0000 +10.0000i
Array Indexing
• Every variable in MATLAB is an array that can hold many numbers. When
you want to access selected elements of an array, use indexing.
A = magic(4)
• There are two ways to refer to a particular element in an array. The most
common way is to specify row and column subscripts, such as
A(4,2)
ans= 14
• you can specify elements outside the current dimensions. The size of the array
increases to accommodate the newcomers.
A(4,5)=17
A=
• To refer to multiple elements of an array, use the
colon operator, which allows you to specify a range
of the form start : end. For example, list the
16 2 3 13 0
elements in the first three rows and the second 5 11 10 8 0
column of A:
9 7 6 12 0
A(1:3,2)
4 14 15 1 17
ans =
2
11
7
• The colon alone, without start or end values, specifies all of the elements in
that dimension. For example, select all the columns in the third row of A:
A(3,:)
ans =
B = 1×11
0 10 20 30 40 50 60 70 80 90 100
Text and Characters
• When you are working with text, enclose sequences of characters in double quotes.
You can assign text to a variable.
t = "Hello, world";
• t is a array, like all MATLAB® variables. The class or data type is string.
• whos t
• Name Size Bytes Class Attributes
seq =
'GCTAGAATCC’;
whos seq
• B = [10 6 4];
• max(A,B)
• ans = 1×3
• 10 6 5
maxA = max(A)
maxA = 5
• When there are multiple output arguments, enclose them in square
brackets:
[maxA,location] = max(A)
maxA = 5
location = 3
2-D and 3-D Plots
• Line Plots
• To create two-dimensional line plots, use the plot function.
• For example, plot the value of the sine function from 0 to 2π:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
• You can label the axes and add a title.
xlabel('x’)
ylabel('sin(x)’)
title('Plot of the Sine Function')
• By adding a third input argument to the plot function, you can plot the same variables using a
red dashed line.
• plot(x,y,'r--')
• x = 0:pi/100:2*pi;
• y = sin(x);
• plot(x,y)
• hold on
• y2 = cos(x);
• plot(x,y2,':')
• legend('sin','cos')
• hold off
3-D Plots
• To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid.
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
• Then, create a surface plot.
surf(X,Y,Z)
Subplots
• You can display multiple plots in different subregions of the same window using
the subplot function.
• The first two inputs to subplot indicate the number of plots in each row and column.
The third input specifies which plot is active. For example, create four plots in a 2-by-2
grid within a figure window.
• t = 0:pi/10:2*pi;
• [X,Y,Z] = cylinder(4*cos(t));
• subplot(2,2,1); mesh(X); title('X');
• subplot(2,2,2); mesh(Y); title('Y');
• subplot(2,2,3); mesh(Z); title('Z');
• subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');