SlideShare a Scribd company logo
MATLAB
Syed Khalid Ahmed
 MATLAB (Matrix Laboratory) [1]
 MATLAB(matrix laboratory) is a multi-paradigm
numerical computing environment and fourth-generation
programming language.
 Developed by Math Works, MATLAB allows matrix
manipulations, plotting of functions and data,
implementation of algorithms, creation of user interfaces,
and interfacing with programs written in other
languages, including C, C++, Java and Fortran.
MATLAB
 MATLAB (Matrix Laboratory) [2]
 Although MATLAB is intended primarily for numerical
computing, an option al toolbox uses the MuPAD
symbolic engine, allowing access to symbolic computing
capabilities. An additional package, Simulink, adds
graphical multi-domain simulation and Model-Based
Design for dynamic and embedded systems.
 In 2004, MATLAB had around one million users across
industry and academia. MATLAB users come from
various backgrounds of engineering, science, and
economics. MATLAB is widely used in academic and
research institutions as well as industrial enterprises.
MATLAB
 Commands
 >>433.12*15.7
 ans=6.8000e+003
 MATLAB spits out the answer to our query conveniently
named ans.
 This is a variable or symbolic name that can be used to
represent the value later.
 >>x=5*6
 X=30
MATLAB
 Commands
 To write the multiplication ab, in MATLAB we type a*b
 For division, the quantity a÷b is typed as a/b. This type
of division is referred to as right division.
 MATLAB also allows another way to enter division,
called left division. We can enter the quantity by a
typing the slash mark used for division in the opposite
way, that is, we use a back slash instead of a forward
slash ab
MATLAB
 Commands
 Exponentiation a to the power b is entered in the
following way a ^ b
 Finally, addition and subtraction are entered in the
usual way
 a + b
 a –b
MATLAB
 Commands
 Individual matrix and vector entries can be referenced
with indices inside parentheses. For example, A(2,3)
denotes the entry in the second row, third column of
matrix A.
 A=[123;456;-179]
 A(2,3)
 Create a column vector, x, with:
 x=[321]’
 Or equivalently:
 x=[3;2;1]
MATLAB
 Commands
 The relational operators in MATLAB are:
 < less than
 > greater than
 <= less than or equal
 >= greater than or equal
 == equal
 ~= not equal
 Note that = is used in an assignment statement whereas
== is a relational operator.
MATLAB
 Commands
 Logical operators:
 Relational operators may be connected by logical
operators:
 & and
 | or
 ~ not
 && short-circuit and
 || short-circuit or
MATLAB
 Commands
 Inner product or dot product
 .* Array multiply:
 X.*Y denotes element-by-element multiplication. X and
Y must have the same dimensions unless one is a
scalar.
 * Matrix multiply:
 X*Y is the matrix product of X and Y. Any scalar (a 1-by-
1 matrix) may multiply anything. Otherwise, the number
of columns of X must equal the number of rows of Y.
MATLAB
 Commands
 /Slash or right matrix divide:
 A/B is the matrix division of B into A, which is roughly the
same as A*INV(B), except it is computed in a different
way. More precisely, A/B = (B'A')'.
 Backslash or left matrix divide:
 AB is the matrix division of A into B, which is roughly the
same as INV(A)*B, except it is computed in a different
way. If A is an N-by-N matrix and B is a column vector
with N components, or a matrix with several such
columns, then X=AB is the solution to the equation
singular
MATLAB
 Commands
 To divide Matrices, element-by-element, the following
formula is useful A./B
 A = [2 4 6 8]; B = [2 2 3 1];
 C = A./B
 % C = [1 2 2 8]
 Array left division is indicated by writing C = A.B (this
is the same as C = B./A):
 C = A.B
 % C = [1.0000 0.5000 0.5000 0.125]
MATLAB
 Commands
 fix()
 fix(X) rounds the elements of X to the nearest integers
towards zero.
 >> fix(5.5)
 ans =
 5
 >> fix(5.9)
 ans =
 5
MATLAB
 Commands
 rand()
 rand():returns an n-by-n matrix containing pseudo
random values drawn from the standard uniform
distribution on the open interval(0,1).
 >> n = rand(1,10)
 n =0.1576 0.9706 0.9572 0.4854 0.8003 0.1419
0.4218 0.9157 0.7922 0.9595
 >> n = fix(10*rand(1,10))
 n =8 9 1 9 6 0 2 5 9 9
MATLAB
 Commands
 Question: Simulate the outcomes of 1000 biased coin
tosses with p[Head] = 0.3
 Solution:
 n = 1000;
 randomNumber= rand(n,1);
 Heads = randomNumber<= 0.3;
 totalNumberOfHeads= sum(Heads);
 probabilityOfHeads= totalNumberOfHeads/n;
MATLAB
 Commands
 Plot(x,y) plot the graph between x and y.
 X and y are the vectors of same lengths.
 >>X=1:10
 >>y=11:20
 >> plot(x,y)
MATLAB
 Commands
 Two or more than two graphs plot on at same time with
hold on function
 x=-5:5
 y=x.*x
 plot(x,y)
 hold on
 a=1:5
 b=1:5
 plot(a,b)
MATLAB
K-MEANS
CLUSTERING
INTRODUCTION-
What is clustering?
 Clustering is the classification of objects into
different groups, or more precisely, the
partitioning of a data set into subsets
(clusters), so that the data in each subset
(ideally) share some common trait - often
according to some defined distance measure.
Common Distance measures:
 Distance measure will determine how the similarity of two
elements is calculated and it will influence the shape of the
clusters.
They include:
1. The Euclidean distance (also called 2-norm distance) is
given by:
2. The Manhattan distance (also called taxicab norm or 1-
norm) is given by:
K-MEANS CLUSTERING
 The k-means algorithm is an algorithm to cluster
n objects based on attributes into k partitions,
where k < n.
K-MEANS CLUSTERING
 Simply speaking k-means clustering is an
algorithm to classify or to group the objects
based on attributes/features into K number of
group.
 K is positive integer number.
 The grouping is done by minimizing the sum
of squares of distances between data and the
corresponding cluster centroid.
How the K-Mean Clustering
algorithm works?
 Step 1: Begin with a decision on the value of k =
number of clusters .
 Step 2: Put any initial partition that classifies the
data into k clusters. You may assign the
training samples randomly,or systematically
as the following:
1.Take the first k training sample as single-
element clusters
2. Assign each of the remaining (N-k) training
sample to the cluster with the nearest
centroid. After each assignment, recompute the
centroid of the gaining cluster.
 Step 3: Take each sample in sequence and
compute its distance from the centroid of
each of the clusters. If a sample is not
currently in the cluster with the closest
centroid, switch this sample to that cluster
and update the centroid of the cluster
gaining the new sample and the cluster
losing the sample.
 Step 4 . Repeat step 3 until convergence is
achieved, that is until a pass through the
training sample causes no new assignments.
Applications of K-Mean
Clustering
 It is relatively efficient and fast. It computes result
at O(tkn), where n is number of objects or points, k
is number of clusters and t is number of iterations.
 k-means clustering can be applied to machine
learning or data mining
 Used on acoustic data in speech understanding to
convert waveforms into one of k categories (known
as Vector Quantization or Image Segmentation).
 Also used for choosing color palettes on old
fashioned graphical display devices and Image
Quantization.
K-Mean Image Processing
Demo
Intro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithm

More Related Content

PPT
Enhance The K Means Algorithm On Spatial Dataset
PPTX
K-means Clustering
PPTX
K MEANS CLUSTERING
PPT
K mean-clustering
DOCX
Neural nw k means
PDF
K means clustering
PPTX
K means clustering | K Means ++
Enhance The K Means Algorithm On Spatial Dataset
K-means Clustering
K MEANS CLUSTERING
K mean-clustering
Neural nw k means
K means clustering
K means clustering | K Means ++

What's hot (20)

PDF
K-means Clustering Algorithm with Matlab Source code
PPTX
K means clustering
PPTX
K-Means manual work
PPT
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
PPTX
K-means clustering algorithm
PDF
K means
PPT
K mean-clustering algorithm
PPT
Cluster analysis using k-means method in R
PDF
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
PPT
Data miningpresentation
PPTX
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
PPTX
K means clustring @jax
PPTX
Kmeans
PPTX
Customer Segmentation using Clustering
PPTX
K means clustering algorithm
PDF
An improvement in k mean clustering algorithm using better time and accuracy
PPTX
K means clustering
PPT
K mean-clustering
PDF
Rough K Means - Numerical Example
K-means Clustering Algorithm with Matlab Source code
K means clustering
K-Means manual work
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
K-means clustering algorithm
K means
K mean-clustering algorithm
Cluster analysis using k-means method in R
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
Data miningpresentation
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
K means clustring @jax
Kmeans
Customer Segmentation using Clustering
K means clustering algorithm
An improvement in k mean clustering algorithm using better time and accuracy
K means clustering
K mean-clustering
Rough K Means - Numerical Example
Ad

Viewers also liked (8)

PPTX
Cardiac Image Analysis based on K Means Clustering
PDF
K means and dbscan
PPTX
A study and comparison of different image segmentation algorithms
PPTX
PPT on BRAIN TUMOR detection in MRI images based on IMAGE SEGMENTATION
PPT
Image segmentation ppt
PPTX
IMAGE SEGMENTATION.
PPT
K means Clustering Algorithm
PPTX
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
Cardiac Image Analysis based on K Means Clustering
K means and dbscan
A study and comparison of different image segmentation algorithms
PPT on BRAIN TUMOR detection in MRI images based on IMAGE SEGMENTATION
Image segmentation ppt
IMAGE SEGMENTATION.
K means Clustering Algorithm
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
Ad

Similar to Intro to MATLAB and K-mean algorithm (20)

PDF
Matlab booklet
PDF
Digital communication lab lectures
PPTX
1. Introduction to Computing - MATLAB.pptx
PDF
A complete introduction on matlab and matlab's projects
PPTX
Basic MATLAB-Presentation.pptx
PDF
Matrix Indexing in MATLAB - MATLAB & Simulink.pdf
PPT
Introduction to Matlab - Basic Functions
PPTX
Introduction to MATLAB Programming for Engineers
PPTX
1. Introduction.pptx
PPT
MATLAB-Introd.ppt
PDF
Introduction to MATLAB
PDF
Dsp manual completed2
PPTX
EPE821_Lecture3.pptx
PPTX
Lecture 3.pptx
PPT
matlab_tutorial.ppt
PPT
matlab_tutorial.ppt
PPT
matlab_tutorial for student in the first
PPT
Matlab intro
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
Matlab booklet
Digital communication lab lectures
1. Introduction to Computing - MATLAB.pptx
A complete introduction on matlab and matlab's projects
Basic MATLAB-Presentation.pptx
Matrix Indexing in MATLAB - MATLAB & Simulink.pdf
Introduction to Matlab - Basic Functions
Introduction to MATLAB Programming for Engineers
1. Introduction.pptx
MATLAB-Introd.ppt
Introduction to MATLAB
Dsp manual completed2
EPE821_Lecture3.pptx
Lecture 3.pptx
matlab_tutorial.ppt
matlab_tutorial.ppt
matlab_tutorial for student in the first
Matlab intro
MatlabIntro.ppt
MatlabIntro.ppt

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Well-logging-methods_new................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
composite construction of structures.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
web development for engineering and engineering
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Model Code of Practice - Construction Work - 21102022 .pdf
additive manufacturing of ss316l using mig welding
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Well-logging-methods_new................
bas. eng. economics group 4 presentation 1.pptx
composite construction of structures.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Foundation to blockchain - A guide to Blockchain Tech
Mechanical Engineering MATERIALS Selection
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
UNIT 4 Total Quality Management .pptx
web development for engineering and engineering
Lecture Notes Electrical Wiring System Components
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Internet of Things (IOT) - A guide to understanding

Intro to MATLAB and K-mean algorithm

  • 2.  MATLAB (Matrix Laboratory) [1]  MATLAB(matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language.  Developed by Math Works, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java and Fortran. MATLAB
  • 3.  MATLAB (Matrix Laboratory) [2]  Although MATLAB is intended primarily for numerical computing, an option al toolbox uses the MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional package, Simulink, adds graphical multi-domain simulation and Model-Based Design for dynamic and embedded systems.  In 2004, MATLAB had around one million users across industry and academia. MATLAB users come from various backgrounds of engineering, science, and economics. MATLAB is widely used in academic and research institutions as well as industrial enterprises. MATLAB
  • 4.  Commands  >>433.12*15.7  ans=6.8000e+003  MATLAB spits out the answer to our query conveniently named ans.  This is a variable or symbolic name that can be used to represent the value later.  >>x=5*6  X=30 MATLAB
  • 5.  Commands  To write the multiplication ab, in MATLAB we type a*b  For division, the quantity a÷b is typed as a/b. This type of division is referred to as right division.  MATLAB also allows another way to enter division, called left division. We can enter the quantity by a typing the slash mark used for division in the opposite way, that is, we use a back slash instead of a forward slash ab MATLAB
  • 6.  Commands  Exponentiation a to the power b is entered in the following way a ^ b  Finally, addition and subtraction are entered in the usual way  a + b  a –b MATLAB
  • 7.  Commands  Individual matrix and vector entries can be referenced with indices inside parentheses. For example, A(2,3) denotes the entry in the second row, third column of matrix A.  A=[123;456;-179]  A(2,3)  Create a column vector, x, with:  x=[321]’  Or equivalently:  x=[3;2;1] MATLAB
  • 8.  Commands  The relational operators in MATLAB are:  < less than  > greater than  <= less than or equal  >= greater than or equal  == equal  ~= not equal  Note that = is used in an assignment statement whereas == is a relational operator. MATLAB
  • 9.  Commands  Logical operators:  Relational operators may be connected by logical operators:  & and  | or  ~ not  && short-circuit and  || short-circuit or MATLAB
  • 10.  Commands  Inner product or dot product  .* Array multiply:  X.*Y denotes element-by-element multiplication. X and Y must have the same dimensions unless one is a scalar.  * Matrix multiply:  X*Y is the matrix product of X and Y. Any scalar (a 1-by- 1 matrix) may multiply anything. Otherwise, the number of columns of X must equal the number of rows of Y. MATLAB
  • 11.  Commands  /Slash or right matrix divide:  A/B is the matrix division of B into A, which is roughly the same as A*INV(B), except it is computed in a different way. More precisely, A/B = (B'A')'.  Backslash or left matrix divide:  AB is the matrix division of A into B, which is roughly the same as INV(A)*B, except it is computed in a different way. If A is an N-by-N matrix and B is a column vector with N components, or a matrix with several such columns, then X=AB is the solution to the equation singular MATLAB
  • 12.  Commands  To divide Matrices, element-by-element, the following formula is useful A./B  A = [2 4 6 8]; B = [2 2 3 1];  C = A./B  % C = [1 2 2 8]  Array left division is indicated by writing C = A.B (this is the same as C = B./A):  C = A.B  % C = [1.0000 0.5000 0.5000 0.125] MATLAB
  • 13.  Commands  fix()  fix(X) rounds the elements of X to the nearest integers towards zero.  >> fix(5.5)  ans =  5  >> fix(5.9)  ans =  5 MATLAB
  • 14.  Commands  rand()  rand():returns an n-by-n matrix containing pseudo random values drawn from the standard uniform distribution on the open interval(0,1).  >> n = rand(1,10)  n =0.1576 0.9706 0.9572 0.4854 0.8003 0.1419 0.4218 0.9157 0.7922 0.9595  >> n = fix(10*rand(1,10))  n =8 9 1 9 6 0 2 5 9 9 MATLAB
  • 15.  Commands  Question: Simulate the outcomes of 1000 biased coin tosses with p[Head] = 0.3  Solution:  n = 1000;  randomNumber= rand(n,1);  Heads = randomNumber<= 0.3;  totalNumberOfHeads= sum(Heads);  probabilityOfHeads= totalNumberOfHeads/n; MATLAB
  • 16.  Commands  Plot(x,y) plot the graph between x and y.  X and y are the vectors of same lengths.  >>X=1:10  >>y=11:20  >> plot(x,y) MATLAB
  • 17.  Commands  Two or more than two graphs plot on at same time with hold on function  x=-5:5  y=x.*x  plot(x,y)  hold on  a=1:5  b=1:5  plot(a,b) MATLAB
  • 19. INTRODUCTION- What is clustering?  Clustering is the classification of objects into different groups, or more precisely, the partitioning of a data set into subsets (clusters), so that the data in each subset (ideally) share some common trait - often according to some defined distance measure.
  • 20. Common Distance measures:  Distance measure will determine how the similarity of two elements is calculated and it will influence the shape of the clusters. They include: 1. The Euclidean distance (also called 2-norm distance) is given by: 2. The Manhattan distance (also called taxicab norm or 1- norm) is given by:
  • 21. K-MEANS CLUSTERING  The k-means algorithm is an algorithm to cluster n objects based on attributes into k partitions, where k < n.
  • 22. K-MEANS CLUSTERING  Simply speaking k-means clustering is an algorithm to classify or to group the objects based on attributes/features into K number of group.  K is positive integer number.  The grouping is done by minimizing the sum of squares of distances between data and the corresponding cluster centroid.
  • 23. How the K-Mean Clustering algorithm works?
  • 24.  Step 1: Begin with a decision on the value of k = number of clusters .  Step 2: Put any initial partition that classifies the data into k clusters. You may assign the training samples randomly,or systematically as the following: 1.Take the first k training sample as single- element clusters 2. Assign each of the remaining (N-k) training sample to the cluster with the nearest centroid. After each assignment, recompute the centroid of the gaining cluster.
  • 25.  Step 3: Take each sample in sequence and compute its distance from the centroid of each of the clusters. If a sample is not currently in the cluster with the closest centroid, switch this sample to that cluster and update the centroid of the cluster gaining the new sample and the cluster losing the sample.  Step 4 . Repeat step 3 until convergence is achieved, that is until a pass through the training sample causes no new assignments.
  • 26. Applications of K-Mean Clustering  It is relatively efficient and fast. It computes result at O(tkn), where n is number of objects or points, k is number of clusters and t is number of iterations.  k-means clustering can be applied to machine learning or data mining  Used on acoustic data in speech understanding to convert waveforms into one of k categories (known as Vector Quantization or Image Segmentation).  Also used for choosing color palettes on old fashioned graphical display devices and Image Quantization.

Editor's Notes

  • #28: %Image 1 k=2,5,10% k=2; A=imread(&amp;apos;pic1.jpg&amp;apos;); A=im2double(A); RGB_matrix = reshape(A,size(A,1)*size(A,2),size(A,3)); clusterPointAllocationMatrix=zeros(size(RGB_matrix ,1),1); p_centroids=zeros(k,3);%previous centroids% centroids = zeros(k,3); %pick random cluster points from matrix% for i=1:k centroids(i,:)=RGB_matrix(randi([1 size(RGB_matrix ,1)],1,1),:); end [r,c]=size(RGB_matrix); %1st column=sum of R ;2nd column=sum of G;3rd cloumn =sum of B; column 4= number of values; index number represent the group number% pointsInfo = zeros(k,4); while ~isequal(centroids,p_centroids) p_centroids=centroids; pointsInfo = zeros(k,4); for j=1:r close=inf; group=0; point1=RGB_matrix(j,:); for l=1:k point2=centroids(l,:); dist=sqrt((point1(1,1)-point2(1,1))^2 + (point1(1,2)-point2(1,2))^2 + (point1(1,3)-point2(1,3))^2); if( dist &amp;lt; close ) close=dist; group=l; end end clusterPointAllocationMatrix(j,1)=group; pointsInfo(group,1)=pointsInfo(group,1)+point1(1,1);%R% pointsInfo(group,2)=pointsInfo(group,2)+point1(1,2);%G% pointsInfo(group,3)=pointsInfo(group,3)+point1(1,3);%B% pointsInfo(group,4)=pointsInfo(group,4)+1;%number of values related to that group% end %updation of centroids% for m=1:k centroids(m,1)=double(pointsInfo(m,1)/pointsInfo(m,4)); centroids(m,2)=double(pointsInfo(m,2)/pointsInfo(m,4)); centroids(m,3)=double(pointsInfo(m,3)/pointsInfo(m,4)); end end newRGB_matrix =zeros(r,c); for o=1:r newRGB_matrix(o,:)=centroids(clusterPointAllocationMatrix(o,1),:); end B=reshape(newRGB_matrix, size(A,1), size(A,2), size(A,3)); C=im2uint8(B) imshow(C);