SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Introduction to MATLAB
                        (Basics)




Reference from:
Azernikov Sergei
mesergei@tx.technion.ac.il
MATLAB Basics

• Where to get help?

1) In MATLAB’s prompt type:
   help, lookfor,helpwin, helpdesk, demos.
2) On the Web:
   http://www.mathworks.com/support

                                             2
MATLAB’s Workspace

•   who,whos - current variables in workspace
•   save - save workspace variables to *.mat file
•   load - load variables from *.mat file
•   clear all - clear workspace variables




                                                3
Matrices in MATLAB


• Matrix is a main MATLAB’s data type
• How to build a matrix?
  A = [ 1 2 3; 4 5 6; 7 8 9 ];
Creates matrix A with size 3x3.
• Special matrices :
  zeros(n,m), ones(n,m),eye (n,m)
                                        4
Basic Operations on Matrices

• All the operators in MATLAB defined on
  matrices : +, -, *, /, ^, sqrt, sin, cos etc.
• Element wise operators defined with
  preceding dot : .*, ./, .^ .
• size(A) - size vector
• sum(A) - columns sums vector
• sum(sum(A)) - all the elements sum
                                                  5
Logical Conditions


• == , < , > , (not equal)~= ,(not)~

• find(‘condition’) - Returns indexes of A’s
  elements that satisfies the condition.


                                           6
Logical Conditions(cont.)

• Example:
>> A = [1 2; 3 4], I = find(A<4)

A=

     1   2
     3   4



I=

     1
     2
     3
                                   7
Flow Control


• MATLAB has five flow control constructs:
       – if statements
       – switch statements
       – for loops
       – while loops
       – break statements

                                             8
if
• IF statement condition.
  The general form of the IF statement is
   IF expression
    statements
   ELSEIF expression
    statements
   ELSE
    statements
   END

                                            9
if(cont.)

• Example:
    if I == J
      A(I,J) = 2;
    elseif abs(I-J) == 1
      A(I,J) = -1;
    else
      A(I,J) = 0;
    end

                           10
switch

• SWITCH - Switch among several cases based on expression.
• The general form of the SWITCH statement is:
   SWITCH switch_expr
     CASE case_expr,
        statement, ..., statement
     CASE {case_expr1, case_expr2, case_expr3,...}
        statement, ..., statement
    ...
     OTHERWISE,
        statement, ..., statement
   END                                                  11
switch (cont.)


•Note:
Only the statements between the matching
CASE and the next CASE, OTHERWISE, or END are executed.
Unlike C, the SWITCH statement does not fall through
(so BREAKs are unnecessary).




                                                  12
for


• FOR Repeat statements a specific number
  of times.

• The general form of a FOR statement is:
   FOR variable = expr, statement, ..., END


                                              13
for (cont.)

• Example:
  FOR I = 1:N,
      FOR J = 1:N,
         A(I,J) = 1/(I+J-1);
      END
  END

                               14
while


• WHILE Repeat statements an indefinite
  number of times.
• The general form of a WHILE statement is:
  WHILE expression
     statements
  END
                                         15
while (cont.)

• Example:
  E = 0*A; F = E + eye(size(E)); N = 1;
  while norm(E+F-E,1) > 0,
         E = E + F;
         F = A*F/N;
         N = N + 1;
  end
                                          16
Scripts and Functions


• There are two kinds of M-files:
        – Scripts, which do not accept input
          arguments or return output arguments. They
          operate on data in the workspace.
        – Functions, which can accept input
          arguments and return output arguments.
          Internal variables are local to the function.

                                                      17
Functions in MATLAB


• FUNCTION Add new function.

• New functions may be added to MATLAB'      s
  vocabulary if they are expressed in terms of
  other existing functions.

                                            18
Functions in MATLAB (cont.)
• Example :
 The existence of a file
 on disk called STAT.M with:
 function [mean,stdev] = stat(x)
 %STAT Interesting statistics.
 n = length(x);
 mean = sum(x) / n;
 stdev = sqrt(sum((x - mean).^2)/n);
 defines a new function called STAT that calculates the
 mean and standard deviation of a vector.
                                                          19
Visualization and Graphics
•   plot(x,y), plot(x,sin(x)) - plot 1-D function
•   figure , figure(k) - open a new figure
•   hold on, hold off - refreshing
•   mesh(x_ax,y_ax,z_mat) - view surface
•   contour(z_mat) - view z as top. map
•   subplot(3,1,2) - locate several plots in figure
•   axis([xmin xmax ymin ymax]) - change axes
•   title(‘figure title’) - add title to figure
                                                      20
Image Proc. with MATLAB
(Please refer to Matlab Demo for more details of Image
                  Processing Tool Box)
What Is the Image Processing Toolbox?

• The Image Processing Toolbox is a collection of functions
  that extend the capability of the MATLAB ® numeric
  computing environment. The toolbox supports a wide
  range of image processing operations, including:
   –   Geometric operations
   –   Neighborhood and block operations
   –   Linear filtering and filter design
   –   Transforms
   –   Image analysis and enhancement
   –   Binary image operations
   –   Region of interest operations
                                                              22
MATLAB Image Types


•   Indexed images     : m-by-3 color map
•   Intensity images   : [0,1] or uint8
•   Binary images      : {0,1}
•   RGB images         : m-by-n-by-3



                                            23
Indexed Images
  » [x,map] = imread('
                     trees.tif'
                              );
  » imshow(x,map);




                                   24
Intensity Images
» image = ind2gray(x,map);
» imshow(image);




                             25
Binary Images
» imshow(edge(image));




                         26
RGB Images




             27
Image Display

•   image - create and display image object
•   imagesc - scale and display as image
•   imshow - display image
•   colorbar - display colorbar
•   getimage- get image data from axes
•   truesize - adjust display size of image
•   zoom - zoom in and zoom out of 2D plot
                                          28
Some Points to Note

Pixel values are accessed as matrix elements.
  2D Image with intensity values: I(row,col)
  2D RGB images I(row,col,color)
    - Color : Red = 1; Green = 2 ; Blue = 3

Displaying images
   figure, imshow(I)

Displaying pixel position and intensity information
   pixval on
                                                 29
Points to Note

 All arithmetic operations performed on matrices
may be performed on images
 After processing, an image matrix can be written
to an output image file with the imwrite function
  - imwrite(I,map,’filename’,’fmt’)
 Without the map argument, the image data is
supposed to be grayscale or RGB.
 The format ‘fmt’ needs to support the particular
type of image
                                               30
Image Conversion
•   gray2ind - intensity image to index image
•   im2bw      - image to binary
•   im2double - image to double precision
•   im2uint8 - image to 8-bit unsigned integers
•   im2uint16 - image to 16-bit unsigned integers
•   ind2gray - indexed image to intensity image
•   mat2gray - matrix to intensity image
•   rgb2gray - RGB image to grayscale
•   rgb2ind - RGB image to indexed image
                                                    31
% Working with Images (example)
[I,map]=imread('
               trees.tif'
                        );                        % read a TIFF image
figure, imshow(I,map)                             % display it as indexed image
I2=ind2gray(I,map);                               % convert it to grayscale
figure
colormap(' )
         gray'                                    % use gray colormap
imagesc(I2,[0 1])                                 % scale data to use full colormap
                                                  % for values between 0 and 1
axis('
     image'
          )                                       % make displayed aspect ratio %proportional
                                                  % to image dimensions
I=imread(‘moon.jpg' % read a JPEG image into 3D
                  );                              %array
figure
imshow(I)
rect=getrect;                                     % select rectangle
I2=imcrop(I,rect);                                % crop
I2=rgb2gray(I2);                                  % convert cropped image to grayscale
imagesc(I2)                                       % scale data to use full colormap
                                                                                          32
% between min and max values in I2
colormap(' )
         gray'
colorbar                      % turn on color bar
pixval                        % display pixel values interactively
truesize                      % display at resolution of one %screen pixel
                              % per image pixel
truesize(2*size(I2))          % display at resolution of two %screen pixels
                              % per image pixel
I3=imresize(I2,0.5,' );
                   bil'       % resize by 50% using bilinear
                              % interpolation
I3=imrotate(I2,45,' );
                  bil'        % rotate 45 degrees and crop to
                              % original size
I3=double(I2);                % convert from uint8 to double, to %allow
% math operations
imagesc(I3.^2)                % display squared image (pixel-wise)
imagesc(log(I3))              % display log of image

                                                                              33
MATLAB Resources on the Internet
http://www.mathworks.com/products/demos/#
http://www.math.siu.edu/MATLAB/tutorials.html
http://math.ucsd.edu/~driver/21d -s99/MATLAB-primer.html
http://www-cse.ucsd.edu/~sjb/classes/MATLAB/MATLAB.intro.html
http://www.mit.edu/~pwb/cssm/
http://www.mathworks.com
Interesting and very complete tutorials in:
http://www.mathworks.com/academia/student_center/tutorials/la
unchpad.html
http://www.mathworks.com/matlabcentral/fileexchange

                                                           34
Getting started with MATLAB
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/learn_matlab.shtml

MATLAB tutorial
  http://www.math.mtu.edu/~msgocken/intro/intro.html
  http://amath.colorado.edu/scico/tutorials/matlab/

MATLAB helpdesk
  http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml

MATLAB Primer
  ftp://ftp.eng.auburn.edu/pub/sjreeves/matlab_primer_40.pdf




                                                                            35

Contenu connexe

Tendances

Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMohd Esa
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabBilawalBaloch1
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabTarun Gehlot
 
Matlab tme series benni
Matlab tme series benniMatlab tme series benni
Matlab tme series bennidvbtunisia
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrixSaidur Rahman
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlabaman gupta
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Randa Elanwar
 
MATLAB BASICS
MATLAB BASICSMATLAB BASICS
MATLAB BASICSbutest
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABSarah Hussein
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingDr. Manjunatha. P
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABBhavesh Shah
 
Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)Retheesh Raj
 
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to MatlabTariq kanher
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 

Tendances (20)

Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab
MatlabMatlab
Matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab Basic Tutorial
Matlab Basic TutorialMatlab Basic Tutorial
Matlab Basic Tutorial
 
Matlab tme series benni
Matlab tme series benniMatlab tme series benni
Matlab tme series benni
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrix
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4
 
MATLAB BASICS
MATLAB BASICSMATLAB BASICS
MATLAB BASICS
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
MATLAB INTRODUCTION
MATLAB INTRODUCTIONMATLAB INTRODUCTION
MATLAB INTRODUCTION
 
Matlab Workshop Presentation
Matlab Workshop PresentationMatlab Workshop Presentation
Matlab Workshop Presentation
 
Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)
 
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to Matlab
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 

Similaire à Matlab intro

Image processing tool box.pptx
Image processing tool box.pptxImage processing tool box.pptx
Image processing tool box.pptxAvinashJain66
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfssuserff72e4
 
Image processing basics using matlab
Image processing basics using matlabImage processing basics using matlab
Image processing basics using matlabAnkur Tyagi
 
Dip digital image 3
Dip digital image 3Dip digital image 3
Dip digital image 3Shajun Nisha
 
image_enhancement_spatial
 image_enhancement_spatial image_enhancement_spatial
image_enhancement_spatialhoneyjecrc
 
Image Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingImage Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingAshok Kumar
 
Image processing for robotics
Image processing for roboticsImage processing for robotics
Image processing for roboticsSALAAMCHAUS
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islamIslam Alabbasy
 
Working with images in matlab graphics
Working with images in matlab graphicsWorking with images in matlab graphics
Working with images in matlab graphicsmustafa_92
 
Performance Anaysis for Imaging System
Performance Anaysis for Imaging SystemPerformance Anaysis for Imaging System
Performance Anaysis for Imaging SystemVrushali Lanjewar
 
Image processing using matlab
Image processing using matlab Image processing using matlab
Image processing using matlab SangeethaSasi1
 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkTUOS-Sam
 
Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)Moe Moe Myint
 

Similaire à Matlab intro (20)

MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Image processing tool box.pptx
Image processing tool box.pptxImage processing tool box.pptx
Image processing tool box.pptx
 
Image processing in MATLAB
Image processing in MATLABImage processing in MATLAB
Image processing in MATLAB
 
Image Processing Using MATLAB
Image Processing Using MATLABImage Processing Using MATLAB
Image Processing Using MATLAB
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
 
Image processing basics using matlab
Image processing basics using matlabImage processing basics using matlab
Image processing basics using matlab
 
Primitives
PrimitivesPrimitives
Primitives
 
Dip digital image 3
Dip digital image 3Dip digital image 3
Dip digital image 3
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
image_enhancement_spatial
 image_enhancement_spatial image_enhancement_spatial
image_enhancement_spatial
 
Image Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingImage Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image Processing
 
Image processing for robotics
Image processing for roboticsImage processing for robotics
Image processing for robotics
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
 
Working with images in matlab graphics
Working with images in matlab graphicsWorking with images in matlab graphics
Working with images in matlab graphics
 
Performance Anaysis for Imaging System
Performance Anaysis for Imaging SystemPerformance Anaysis for Imaging System
Performance Anaysis for Imaging System
 
Image processing using matlab
Image processing using matlab Image processing using matlab
Image processing using matlab
 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
 
Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)
 
Dip day1&2
Dip day1&2Dip day1&2
Dip day1&2
 
Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)
 

Dernier

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 

Dernier (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 

Matlab intro

  • 1. Introduction to MATLAB (Basics) Reference from: Azernikov Sergei mesergei@tx.technion.ac.il
  • 2. MATLAB Basics • Where to get help? 1) In MATLAB’s prompt type: help, lookfor,helpwin, helpdesk, demos. 2) On the Web: http://www.mathworks.com/support 2
  • 3. MATLAB’s Workspace • who,whos - current variables in workspace • save - save workspace variables to *.mat file • load - load variables from *.mat file • clear all - clear workspace variables 3
  • 4. Matrices in MATLAB • Matrix is a main MATLAB’s data type • How to build a matrix? A = [ 1 2 3; 4 5 6; 7 8 9 ]; Creates matrix A with size 3x3. • Special matrices : zeros(n,m), ones(n,m),eye (n,m) 4
  • 5. Basic Operations on Matrices • All the operators in MATLAB defined on matrices : +, -, *, /, ^, sqrt, sin, cos etc. • Element wise operators defined with preceding dot : .*, ./, .^ . • size(A) - size vector • sum(A) - columns sums vector • sum(sum(A)) - all the elements sum 5
  • 6. Logical Conditions • == , < , > , (not equal)~= ,(not)~ • find(‘condition’) - Returns indexes of A’s elements that satisfies the condition. 6
  • 7. Logical Conditions(cont.) • Example: >> A = [1 2; 3 4], I = find(A<4) A= 1 2 3 4 I= 1 2 3 7
  • 8. Flow Control • MATLAB has five flow control constructs: – if statements – switch statements – for loops – while loops – break statements 8
  • 9. if • IF statement condition. The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END 9
  • 10. if(cont.) • Example: if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end 10
  • 11. switch • SWITCH - Switch among several cases based on expression. • The general form of the SWITCH statement is: SWITCH switch_expr CASE case_expr, statement, ..., statement CASE {case_expr1, case_expr2, case_expr3,...} statement, ..., statement ... OTHERWISE, statement, ..., statement END 11
  • 12. switch (cont.) •Note: Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed. Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary). 12
  • 13. for • FOR Repeat statements a specific number of times. • The general form of a FOR statement is: FOR variable = expr, statement, ..., END 13
  • 14. for (cont.) • Example: FOR I = 1:N, FOR J = 1:N, A(I,J) = 1/(I+J-1); END END 14
  • 15. while • WHILE Repeat statements an indefinite number of times. • The general form of a WHILE statement is: WHILE expression statements END 15
  • 16. while (cont.) • Example: E = 0*A; F = E + eye(size(E)); N = 1; while norm(E+F-E,1) > 0, E = E + F; F = A*F/N; N = N + 1; end 16
  • 17. Scripts and Functions • There are two kinds of M-files: – Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace. – Functions, which can accept input arguments and return output arguments. Internal variables are local to the function. 17
  • 18. Functions in MATLAB • FUNCTION Add new function. • New functions may be added to MATLAB' s vocabulary if they are expressed in terms of other existing functions. 18
  • 19. Functions in MATLAB (cont.) • Example : The existence of a file on disk called STAT.M with: function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n); defines a new function called STAT that calculates the mean and standard deviation of a vector. 19
  • 20. Visualization and Graphics • plot(x,y), plot(x,sin(x)) - plot 1-D function • figure , figure(k) - open a new figure • hold on, hold off - refreshing • mesh(x_ax,y_ax,z_mat) - view surface • contour(z_mat) - view z as top. map • subplot(3,1,2) - locate several plots in figure • axis([xmin xmax ymin ymax]) - change axes • title(‘figure title’) - add title to figure 20
  • 21. Image Proc. with MATLAB (Please refer to Matlab Demo for more details of Image Processing Tool Box)
  • 22. What Is the Image Processing Toolbox? • The Image Processing Toolbox is a collection of functions that extend the capability of the MATLAB ® numeric computing environment. The toolbox supports a wide range of image processing operations, including: – Geometric operations – Neighborhood and block operations – Linear filtering and filter design – Transforms – Image analysis and enhancement – Binary image operations – Region of interest operations 22
  • 23. MATLAB Image Types • Indexed images : m-by-3 color map • Intensity images : [0,1] or uint8 • Binary images : {0,1} • RGB images : m-by-n-by-3 23
  • 24. Indexed Images » [x,map] = imread(' trees.tif' ); » imshow(x,map); 24
  • 25. Intensity Images » image = ind2gray(x,map); » imshow(image); 25
  • 28. Image Display • image - create and display image object • imagesc - scale and display as image • imshow - display image • colorbar - display colorbar • getimage- get image data from axes • truesize - adjust display size of image • zoom - zoom in and zoom out of 2D plot 28
  • 29. Some Points to Note Pixel values are accessed as matrix elements. 2D Image with intensity values: I(row,col) 2D RGB images I(row,col,color) - Color : Red = 1; Green = 2 ; Blue = 3 Displaying images figure, imshow(I) Displaying pixel position and intensity information pixval on 29
  • 30. Points to Note All arithmetic operations performed on matrices may be performed on images After processing, an image matrix can be written to an output image file with the imwrite function - imwrite(I,map,’filename’,’fmt’) Without the map argument, the image data is supposed to be grayscale or RGB. The format ‘fmt’ needs to support the particular type of image 30
  • 31. Image Conversion • gray2ind - intensity image to index image • im2bw - image to binary • im2double - image to double precision • im2uint8 - image to 8-bit unsigned integers • im2uint16 - image to 16-bit unsigned integers • ind2gray - indexed image to intensity image • mat2gray - matrix to intensity image • rgb2gray - RGB image to grayscale • rgb2ind - RGB image to indexed image 31
  • 32. % Working with Images (example) [I,map]=imread(' trees.tif' ); % read a TIFF image figure, imshow(I,map) % display it as indexed image I2=ind2gray(I,map); % convert it to grayscale figure colormap(' ) gray' % use gray colormap imagesc(I2,[0 1]) % scale data to use full colormap % for values between 0 and 1 axis(' image' ) % make displayed aspect ratio %proportional % to image dimensions I=imread(‘moon.jpg' % read a JPEG image into 3D ); %array figure imshow(I) rect=getrect; % select rectangle I2=imcrop(I,rect); % crop I2=rgb2gray(I2); % convert cropped image to grayscale imagesc(I2) % scale data to use full colormap 32
  • 33. % between min and max values in I2 colormap(' ) gray' colorbar % turn on color bar pixval % display pixel values interactively truesize % display at resolution of one %screen pixel % per image pixel truesize(2*size(I2)) % display at resolution of two %screen pixels % per image pixel I3=imresize(I2,0.5,' ); bil' % resize by 50% using bilinear % interpolation I3=imrotate(I2,45,' ); bil' % rotate 45 degrees and crop to % original size I3=double(I2); % convert from uint8 to double, to %allow % math operations imagesc(I3.^2) % display squared image (pixel-wise) imagesc(log(I3)) % display log of image 33
  • 34. MATLAB Resources on the Internet http://www.mathworks.com/products/demos/# http://www.math.siu.edu/MATLAB/tutorials.html http://math.ucsd.edu/~driver/21d -s99/MATLAB-primer.html http://www-cse.ucsd.edu/~sjb/classes/MATLAB/MATLAB.intro.html http://www.mit.edu/~pwb/cssm/ http://www.mathworks.com Interesting and very complete tutorials in: http://www.mathworks.com/academia/student_center/tutorials/la unchpad.html http://www.mathworks.com/matlabcentral/fileexchange 34
  • 35. Getting started with MATLAB http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/learn_matlab.shtml MATLAB tutorial http://www.math.mtu.edu/~msgocken/intro/intro.html http://amath.colorado.edu/scico/tutorials/matlab/ MATLAB helpdesk http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml MATLAB Primer ftp://ftp.eng.auburn.edu/pub/sjreeves/matlab_primer_40.pdf 35