Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
clustering , segmentation

Subject: clustering , segmentation

From: mshahrashoub shahrashoub

Date: 1 Sep, 2010 10:34:21

Message: 1 of 8

Hi,

Does anybody have an idea about, How can I cluster or segmentation, my vectorized data.

Example : Data = [1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4] ;

and I want to make 4 cluster. How can make it ?


I have Matlab R2010a with Image Processing toolbox.

Thanks in advance

Subject: clustering , segmentation

From: ImageAnalyst

Date: 1 Sep, 2010 11:45:01

Message: 2 of 8

bwlabel(), bwconncomp() depending on what you want to do.

Subject: clustering , segmentation

From: Ömer KAYA

Date: 2 Sep, 2010 07:15:19

Message: 3 of 8

" mshahrashoub shahrashoub" <shahrashoub@yahoo.com> wrote in message <i5la7d$m4g$1@fred.mathworks.com>...
> Hi,
>
> Does anybody have an idea about, How can I cluster or segmentation, my vectorized data.
>
> Example : Data = [1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4] ;
>
> and I want to make 4 cluster. How can make it ?
>
>
> I have Matlab R2010a with Image Processing toolbox.
>
> Thanks in advance

Hello;

A=0;
B=0;
C=0;
D=0;
%% defining the for loop matrix
[f,Matrix] = size(Data);
clear f;
%% Defining the loop
for i= 1 :4;
    for j=1 : Matrix;
        if Data(1,j)== 1 ; %the cluster Mid Point (can calculate on avarage)
            A(1,j) = Data(1,j);
        elseif Data(1,j) == 2; %Second Cluster
            B(1,j)=Data(1,j);
        elseif Data(1,j) == 3; %Third
            C(1,j)=Data(1,j);
        elseif Data(1,j) == 4; %Forth Cluster
            D(1,j)=Data(1,j);
        end
    end
end
%%
use this code and you will separate in in clusters but the problem is i couldn't make the process start from the beginning...

so the last cluster is the same size of the input data; there should be another person can figure out how to deal with the rest.

Regads

Ömer KAYA

Middle East Technical Universty

Subject: clustering , segmentation

From: Cris Luengo

Date: 2 Sep, 2010 08:31:19

Message: 4 of 8

"Ömer KAYA" <e170531@metu.edu.tr> wrote in message <i5niu7$g6$1@fred.mathworks.com>...
> " mshahrashoub shahrashoub" <shahrashoub@yahoo.com> wrote in message <i5la7d$m4g$1@fred.mathworks.com>...
> > Hi,
> >
> > Does anybody have an idea about, How can I cluster or segmentation, my vectorized data.
> >
> > Example : Data = [1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4] ;
> >
> > and I want to make 4 cluster. How can make it ?
> >
> >
> > I have Matlab R2010a with Image Processing toolbox.
> >
> > Thanks in advance
>
> Hello;
>
> A=0;
> B=0;
> C=0;
> D=0;
> %% defining the for loop matrix
> [f,Matrix] = size(Data);
> clear f;
> %% Defining the loop
> for i= 1 :4;
> for j=1 : Matrix;
> if Data(1,j)== 1 ; %the cluster Mid Point (can calculate on avarage)
> A(1,j) = Data(1,j);
> elseif Data(1,j) == 2; %Second Cluster
> B(1,j)=Data(1,j);
> elseif Data(1,j) == 3; %Third
> C(1,j)=Data(1,j);
> elseif Data(1,j) == 4; %Forth Cluster
> D(1,j)=Data(1,j);
> end
> end
> end
> %%
> use this code and you will separate in in clusters but the problem is i couldn't make the process start from the beginning...
>
> so the last cluster is the same size of the input data; there should be another person can figure out how to deal with the rest.
>
> Regads
>
> Ömer KAYA
>
> Middle East Technical Universty

Ömer, your code does the same as:
   A = (Data==1)*1;
   B = (Data==2)*2;
   C = (Data==3)*3;
   D = (Data==4)*4;
but I cannot imagine that this is what the original poster wants.

 mshahrashoub, what is it that you want as output?

Cheers,
Cris.

Subject: clustering , segmentation

From: mshahrashoub shahrashoub

Date: 2 Sep, 2010 09:29:21

Message: 5 of 8

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i5nncn$k6h$1@fred.mathworks.com>...
> "Ömer KAYA" <e170531@metu.edu.tr> wrote in message <i5niu7$g6$1@fred.mathworks.com>...
> > " mshahrashoub shahrashoub" <shahrashoub@yahoo.com> wrote in message <i5la7d$m4g$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > Does anybody have an idea about, How can I cluster or segmentation, my vectorized data.
> > >
> > > Example : Data = [1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4] ;
> > >
> > > and I want to make 4 cluster. How can make it ?
> > >
> > >
> > > I have Matlab R2010a with Image Processing toolbox.
> > >
> > > Thanks in advance
> >
> > Hello;
> >
> > A=0;
> > B=0;
> > C=0;
> > D=0;
> > %% defining the for loop matrix
> > [f,Matrix] = size(Data);
> > clear f;
> > %% Defining the loop
> > for i= 1 :4;
> > for j=1 : Matrix;
> > if Data(1,j)== 1 ; %the cluster Mid Point (can calculate on avarage)
> > A(1,j) = Data(1,j);
> > elseif Data(1,j) == 2; %Second Cluster
> > B(1,j)=Data(1,j);
> > elseif Data(1,j) == 3; %Third
> > C(1,j)=Data(1,j);
> > elseif Data(1,j) == 4; %Forth Cluster
> > D(1,j)=Data(1,j);
> > end
> > end
> > end
> > %%
> > use this code and you will separate in in clusters but the problem is i couldn't make the process start from the beginning...
> >
> > so the last cluster is the same size of the input data; there should be another person can figure out how to deal with the rest.
> >
> > Regads
> >
> > Ömer KAYA
> >
> > Middle East Technical Universty
>
> Ömer, your code does the same as:
> A = (Data==1)*1;
> B = (Data==2)*2;
> C = (Data==3)*3;
> D = (Data==4)*4;
> but I cannot imagine that this is what the original poster wants.
>
> mshahrashoub, what is it that you want as output?
>
> Cheers,
> Cris.

Thanks everybody for helps.
The code written by Ömer works fine with given example. But the problem is, my data set not like this :

For example :

If I have a vector like :

X = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 22 44 1 1 ]

To assess 4 cluster for given vector how this could work. In addition of this Chris you define A = 1 , B = 2 , C =3, D =4 But How can algorithm recognize this numbers. System should be robust. I think I need to use density to to calculate A,B,C,D numbers.

What about this code:
How it is works :
DataM = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 22 44 1 1 ] % vector example
I am trying to make 4 cluster

%%
So first of all i am dividing vector to number of cluster
Size of vector : 20 Cluster size : 4
Each cluster will have 5 values.

%Sequentially I am adding the numbers amount of cluster
DataM = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 23 67 68 69
then dividing sum of cluster with number of elements in cluster
DataM = 15/5 15/5 15/5 328/5
DataM = 3 3 3 65

After this I can divide it more(simplification ) but this results is enough for me.
Now I got 4 cluster values
%%
> > > a=0;

> > > i = 0;


> > > L = size(DataM,1);
> > > for i=0:i+4:L; %% "L" is a size of the vector


> > > a = a+1; %% "a" is a size of cluster

> > > valmatrix = zeros(L/a,1); %% Create empty matrix to calculate cumulative of the cluster

> > > valmatrix(i+1 )=DataM(i+1 ,x,y); %% start the first element with first element of the vector Example 1 1 1 1 to 1 2 3 4 5 : First digit should be same

> > > for z = 2+i:i+4,

> > > valmatrix(z)=valmatrix((z) -1) + DataM(z,x,y); % calculate rest of the cumulative

> > > end

> > > valmatrix = sum(valmatrix) / (L/a);

> > > Output(a ,x,y) = valmatrix ;

> > > end


Output should be DataM = 3 3 3 65 something like this.

I dont know this code should work probably

Thanks in advance
 

Subject: clustering , segmentation

From: ImageAnalyst

Date: 2 Sep, 2010 10:47:00

Message: 6 of 8

Looks like you're just trying to compute the mean value of each
cluster. The problem is, your clusters aren't separated. How are
your first 3 groups of 5 1's separated? They're not - they're
immediately adjacent to each other and can't be distinguished unless
you say so, like you know in advance where each cluster of 1's starts
and stops.

Normally taking the mean of clusters is a two or 3 lines process.
Just call bwlabel and regionprops like I said originally.

Subject: clustering , segmentation

From: Ömer KAYA

Date: 2 Sep, 2010 11:10:23

Message: 7 of 8

> Thanks everybody for helps.
> The code written by Ömer works fine with given example. But the problem is, my data set not like this :
>
> For example :
>
> If I have a vector like :
>
> X = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 22 44 1 1 ]
>
> To assess 4 cluster for given vector how this could work. In addition of this Chris you define A = 1 , B = 2 , C =3, D =4 But How can algorithm recognize this numbers. System should be robust. I think I need to use density to to calculate A,B,C,D numbers.
>
> What about this code:
> How it is works :
> DataM = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 22 44 1 1 ] % vector example
> I am trying to make 4 cluster
>
> %%
> So first of all i am dividing vector to number of cluster
> Size of vector : 20 Cluster size : 4
> Each cluster will have 5 values.
>
> %Sequentially I am adding the numbers amount of cluster
> DataM = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 23 67 68 69
> then dividing sum of cluster with number of elements in cluster
> DataM = 15/5 15/5 15/5 328/5
> DataM = 3 3 3 65
>
> After this I can divide it more(simplification ) but this results is enough for me.
> Now I got 4 cluster values
> %%
> > > > a=0;
>
> > > > i = 0;
>
>
> > > > L = size(DataM,1);
> > > > for i=0:i+4:L; %% "L" is a size of the vector
>
>
> > > > a = a+1; %% "a" is a size of cluster
>
> > > > valmatrix = zeros(L/a,1); %% Create empty matrix to calculate cumulative of the cluster
>
> > > > valmatrix(i+1 )=DataM(i+1 ,x,y); %% start the first element with first element of the vector Example 1 1 1 1 to 1 2 3 4 5 : First digit should be same
>
> > > > for z = 2+i:i+4,
>
> > > > valmatrix(z)=valmatrix((z) -1) + DataM(z,x,y); % calculate rest of the cumulative
>
> > > > end
>
> > > > valmatrix = sum(valmatrix) / (L/a);
>
> > > > Output(a ,x,y) = valmatrix ;
>
> > > > end
>
>
> Output should be DataM = 3 3 3 65 something like this.
>
> I dont know this code should work probably
>
> Thanks in advance

Hi Everyone;

what i understand is that you want to write an algorithm which can calculate the clusters and put them into a separate data(vector lets say); K-Means if i am right.
(luck for you i know data mining :] )

in this case you have to go one by one; In pseudo code;

read the file (xls, text, ext.);
%% look for clusters a (loop approach, near class approach, you have to decide);
look for the vectors (input data) histogram;
hist(data);
% the best of determining the dense points is histogram or;
% selecting first 40 (or your own) then getting the mean value and then checking %this value with respect to others with the distance (absolute distance to get rid %of the minus sign) ;
%% for histogram;
select the pic points;
choose them as clusters (with respect to distance)
then send them one by one to a data file)

%% For data select
for i= 0 to eof;
 select first 40 (or eof/cluster number);
end
centroid = Sum(A(the data with 40 values) )/40;
centroid = centroid of a point.
%here your have a centroid:
for i = 0 to 40
Distance(1,i)= centroid - A(1,i);
end
% now you must see the file

and the process goes just like this;

hope it helps ;

google:

K-means distance approaches (techniques)
codeding distance

Have a nice day

Ömer KAYA

METU

Subject: clustering , segmentation

From: mshahrashoub shahrashoub

Date: 2 Sep, 2010 11:54:05

Message: 8 of 8

"Ömer KAYA" <e170531@metu.edu.tr> wrote in message <i5o0mv$f9c$1@fred.mathworks.com>...
> > Thanks everybody for helps.
> > The code written by Ömer works fine with given example. But the problem is, my data set not like this :
> >
> > For example :
> >
> > If I have a vector like :
> >
> > X = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 22 44 1 1 ]
> >
> > To assess 4 cluster for given vector how this could work. In addition of this Chris you define A = 1 , B = 2 , C =3, D =4 But How can algorithm recognize this numbers. System should be robust. I think I need to use density to to calculate A,B,C,D numbers.
> >
> > What about this code:
> > How it is works :
> > DataM = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 22 44 1 1 ] % vector example
> > I am trying to make 4 cluster
> >
> > %%
> > So first of all i am dividing vector to number of cluster
> > Size of vector : 20 Cluster size : 4
> > Each cluster will have 5 values.
> >
> > %Sequentially I am adding the numbers amount of cluster
> > DataM = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 23 67 68 69
> > then dividing sum of cluster with number of elements in cluster
> > DataM = 15/5 15/5 15/5 328/5
> > DataM = 3 3 3 65
> >
> > After this I can divide it more(simplification ) but this results is enough for me.
> > Now I got 4 cluster values
> > %%
> > > > > a=0;
> >
> > > > > i = 0;
> >
> >
> > > > > L = size(DataM,1);
> > > > > for i=0:i+4:L; %% "L" is a size of the vector
> >
> >
> > > > > a = a+1; %% "a" is a size of cluster
> >
> > > > > valmatrix = zeros(L/a,1); %% Create empty matrix to calculate cumulative of the cluster
> >
> > > > > valmatrix(i+1 )=DataM(i+1 ,x,y); %% start the first element with first element of the vector Example 1 1 1 1 to 1 2 3 4 5 : First digit should be same
> >
> > > > > for z = 2+i:i+4,
> >
> > > > > valmatrix(z)=valmatrix((z) -1) + DataM(z,x,y); % calculate rest of the cumulative
> >
> > > > > end
> >
> > > > > valmatrix = sum(valmatrix) / (L/a);
> >
> > > > > Output(a ,x,y) = valmatrix ;
> >
> > > > > end
> >
> >
> > Output should be DataM = 3 3 3 65 something like this.
> >
> > I dont know this code should work probably
> >
> > Thanks in advance
>
> Hi Everyone;
>
> what i understand is that you want to write an algorithm which can calculate the clusters and put them into a separate data(vector lets say); K-Means if i am right.
> (luck for you i know data mining :] )
>
> in this case you have to go one by one; In pseudo code;
>
> read the file (xls, text, ext.);
> %% look for clusters a (loop approach, near class approach, you have to decide);
> look for the vectors (input data) histogram;
> hist(data);
> % the best of determining the dense points is histogram or;
> % selecting first 40 (or your own) then getting the mean value and then checking %this value with respect to others with the distance (absolute distance to get rid %of the minus sign) ;
> %% for histogram;
> select the pic points;
> choose them as clusters (with respect to distance)
> then send them one by one to a data file)
>
> %% For data select
> for i= 0 to eof;
> select first 40 (or eof/cluster number);
> end
> centroid = Sum(A(the data with 40 values) )/40;
> centroid = centroid of a point.
> %here your have a centroid:
> for i = 0 to 40
> Distance(1,i)= centroid - A(1,i);
> end
> % now you must see the file
>
> and the process goes just like this;
>
> hope it helps ;
>
> google:
>
> K-means distance approaches (techniques)
> codeding distance
>
> Have a nice day
>
> Ömer KAYA
>
> METU


Thanks Ömer, You got what I mean. Many thanks for feedback and solution.

Dear Image analyst, bwlabel and regionprops, I dont know which parameter do i need to use there (for the measurement) Centroid ? convex area etc . I am a bit confused.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us