Lecture 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

IT Workshop

Programming in MATLAB
Lecture-2
Matrix operations

MATLAB allows you to process all of the values in


a matrix using a single arithmetic operator or
function.
a = [1 2 3; 4 5 6; 7 8 10]
a=

1 2 3
4 5 6
7 8 10
Matrix operations

Matrix Addition Sin of Matrix

a + 10 sin(a)
ans = ans =

11 12 13 0.8415 0.9093 0.1411


14 15 16 -0.7568 -0.9589 -0.2794
17 18 20 0.6570 0.9894 -0.5440
Transpose of Matrix

To transpose a matrix, use a single quote ('):

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

To perform element-wise multiplication rather than matrix


multiplication, use the .* operator:

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 =

1 8 27 0.3333 0.6667 1.0000


64 125 216 1.3333 1.6667 2.0000
343 512 1000 2.3333 2.6667 3.3333
Concatenation
Concatenation is the process of joining arrays to make larger ones. In fact, you
made your first array by concatenating its individual elements. The pair of square
brackets [] is the concatenation operator.

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

Complex numbers have both real and imaginary parts,


where the imaginary unit is the square root of -1.

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 =

• The colon operator also allows you to create


9 7 6 12 0
an equally spaced vector of values using the
more general form start : step :end .

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

• t 1x1 174 string


• To add text to the end of a string, use the plus operator, +
f = 71;
c = (f-32)/1.8;
tempText = "Temperature is " + c + "C"
tempText =
"Temperature is 21.6667C"

seq =
'GCTAGAATCC’;
whos seq

Name Size Bytes Class Attributes


seq 1x10 20 char
Calling Functions

• MATLAB® provides a large number of functions that perform computational tasks.


Functions are equivalent to subroutines or methods in other programming languages.

• To call a function, such as max, enclose its input arguments in parentheses:


• A = [1 3 5];
• max(A)
• ans = 5
• If there are multiple input arguments, separate them with commas:

• 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--')

• 'r--' is a line specification. Each specification can include


characters for the line color, style, and marker. A marker
is a symbol that appears at each plotted data point, such
as a +, o, or *. For example, 'g:*' requests a dotted green
line with * markers.
• To add plots to an existing figure, use hold on. Until you use hold off or close the window, all
plots appear in the current figure window.

• 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

• Three-dimensional plots typically display a surface defined by a function in two variables, z =


f(x,y) .

• 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');

You might also like