You are on page 1of 29

Universidad Industrial de Santander

Introduction to MATLAB:

Professor: Henry Arguello Fuentes


Professor Assistants: Andrés Jerez1 & Jhon López2
1 andres.jerez1@correo.uis.edu.co
2 jhon2208456@correo.uis.edu.co

2021
Outline

1 How to install MATLAB?

2 MATLAB Online

3 Overview

4 Variables

5 Matrix

6 Miscellaneous

2/29
How to install MATLAB?

1 Go to https://la.mathworks.com/
2 Click on icon

3 Login or create an account using your institutional email:


@correo.uis.edu.co
4 Follow the activation and verification steps
5 Download the desired MATLAB version choosing the
appropriate operating system
6 Follow the installation steps starting with the option: Log
in with a MathWorks Account
Now you are capable of using MATLAB!

3/29
MATLAB Online

1 Repeat the first three steps above


2 Click on MATLAB Online

Now you are capable of using MATLAB Online!

4/29
What & Why

• Matrix Laboratory
• Dynamically typed language
• Variables require no declaration
• Creation by initialization (x = 10; )
• All variables are treated as matrices
• Scalar: 1 × 1 matrix; Vector: N × 1 or 1 × N matrix
• Calculations are much faster
• Advantages
• Fast implementation and debugging
• Natural matrix operation
• Powerful image processing toolbox

5/29
MATLAB Environment

Current Directory
View Folders and m-�iles

Workspace
View variables
Double click on a variable
to see it in the Array Editor
Editor
Type commands in a Script

Command Window
Type commands

6/29
Defining Variables

C/C++ MATLAB
int a;
a = 1; >> a = 1;
double b; >> b = 2 + 4;
b = 2 + 4;

• Variables are created when they are used.


• All variables are crated as matrices with "some" type
(unless specified).

7/29
Variables

>> a = 1; >> A = [1 2 3] text


A=
1 2 3

>> B = [1 2 3; 4 5 6]
B=
>> b = false; 1 2 3
4 5 6

8/29
Variables

>> C = [1 ; 2 ; 3] text
C=
1
2
3

>> D = [1 2 3]0
D=
1
2
3

9/29
Variables

>> A = 1 : 10
A=
1 2 3 4 5 6 7 8 9 10

>> B = 0 : 2 : 10
B=
0 2 4 6 8 10

>> 1 : 0.5 : 5
ans =
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000

C = 0 Hello World!0 ;
>> C =0 Hello World!0
C=
Hello World!

10/29
Variables

• A = zeros(3);
• B = ones(5);
• C = rand(100, 2);
• D = eye(20);
• E = sprintf(0 %d\n0 , 9)

11/29
Matrix Index

• Matrix indices begin from 1 (not 0!)


• Matrix indices must be positive integers

A= >> A(1, 3) >> A(1) >> A(2)


1 2 3 ans = ans = ans =
4 5 6 3 1 4
7 8 9
>> A(0)
Array indices must be positive integers or logical values.
>> A(1, 4)
Index in position 2 exceeds array bounds (must not exceed 3).

12/29
Matrix Index

A= >> A(2, 2 : 3) >> A(2, 1 : end)


1 2 3 ans = ans =
4 5 6 5 6 4 5 6
7 8 9

>> A(2, :) >> A(2, 1 : 2 : 3) >> A(2, [1 3])


ans = ans = ans =
4 5 6 4 6 4 6
>> A(:)0
ans =
1 2 3 4 5 6 7 8 9

13/29
Matrix Index

Accessing Elements
• A = rand(4);
• A(2, 3)
• A(:, 2)
• A(end, :)
• A([1, 2], [1, 3])
• A(1 : 2, 3 : end)

14/29
Matrix Operations

• + addition
• − subtraction
• ∗ multiplication
• ˆpower
• 0 complex conjugate transpose

15/29
Matrix Operations

Given A and B: A= B=
1 2 3 3 5 2
4 5 6 5 2 8
7 8 9 3 6 9

Addition Subtraction Product Transpose


>> X = A + B >> Y = A − B >> Z = A ∗ B >> T = A0
X= Y= Z= T=

4 7 5 −2 −3 1 22 27 45 1 4 7

9 7 14 −1 3 −2 55 66 102 2 5 8

10 14 18 4 2 0 88 105 159 1 4 9

16/29
Matrix Operations

• .∗ element-wise multiplication
• ./ element-wise division
• .ˆelement-wise power

17/29
Matrix Operations

18/29
Matrix Operations

• A/B Solve linear equation xA = B for x


• A\B Solve linear equation Ax = B for x

19/29
Matrix Concatenation

X = [1 2], Y = [3 4]

>> A = [X Y] >> A = [X ; Y]
A= A=
1 2 3 4 1 2
3 4

20/29
Strings

• A = ’vision and geometry’


• strfind(A, ’geometry’)
• strcmp(A, ’computer vision’)
• strcat(A, ’12345’)
• sprintf(0 I am %02d years old.\n0 , 9)

21/29
Cell and Structure

• Cells
• a = {}; a = cell(1)
• b = {1, 2, 3}
• c = {{1, 2}, 2, {3}}
• d = {’cat’, ’dog’, ’sheep’, ’cow’}
• {’cat’, 4}
• Structures
• A = struct(’name’, ’1.jpg’, ’height’, 640, ’width’, 480);
• b.name = ’1.jpg’

22/29
Operators

• == Equal to
• ∼= Not equal to
• < Strictly smaller
• > Strictly greater
• <= Smaller than or equal to
• >= Greater than or equal to
• & And operator
• | Or operator

23/29
Flow Control

24/29
Implementing Functions

How?
• Create a M-file with the function name
• Use the function definition at the beginning
function out1 = functionname(int1)
Some Matlab Commands;
function out1 = functionname(int1, int2, int3)
Some Matlab Commands;
function [out1, out2] = functionname(int1, int2)
Some Matlab Commands;

Hands-on!
Implement your own function that computes the sum of
the square of each element in a N × N matrix.

25/29
Plotting

Plotting functions
plot, plot3d, bar, area, hist, contour, mesh
>> x = −pi : 0.1 : pi; text
1
>> y = sin(x); 0.8

>> plot(x, y) 0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-4 -3 -2 -1 0 1 2 3 4

26/29
Help & Doc

• >> help functionName


• >> doc functionName

27/29

You might also like