Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Clustering Pairs

Subject: Clustering Pairs

From: Marie

Date: 02 Aug, 2007 02:16:44

Message: 1 of 3

Greetings,
I have a series of pairs of numbers, e.g.:
[1 10;1 11;1 12;2 11;5 10;5 15;6 18]
I want to cluster together all pairs of numbers that contain common values. For example, in this case this would be [1 2 5 10 11 12 15] and [6 18].
Any help is appreciated!
S

Subject: Re: Clustering Pairs

From: us

Date: 02 Aug, 2007 06:16:03

Message: 2 of 3

Marie:
<SNIP down to problem with example...

> [1 10;1 11;1 12;2 11;5 10;5 15;6 18]
> I want to cluster together all pairs of numbers that contain common values. For example, in this case this would be [1 2 5 10 11 12 15] and [6 18]...

there are many solutions to this.
however, your example does not make sense...
can you be more clear?
us

Subject: Re: Clustering Pairs

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 02 Aug, 2007 01:51:12

Message: 3 of 3

In article <f8reqc$o08$1@fred.mathworks.com>, "Marie "
<sarrowsmith@gmail.com> wrote:

> Greetings,
> I have a series of pairs of numbers, e.g.:
> [1 10;1 11;1 12;2 11;5 10;5 15;6 18]
> I want to cluster together all pairs of numbers that contain common
values. For example, in this case this would be [1 2 5 10 11 12 15] and [6
18].
> Any help is appreciated!
> S
----------------------
  I believe you are seeking what in mathematics is known as an equivalence
relation, Marie. For example, the 2 and the 15 are in the same
equivalence class (cluster) because of a chain of pairings connecting
them: (2,11), (11,1), (1,10), (10,5), and (5,15). (Note that order within
the pairs is not significant.) If I am interpreting you correctly, the
following is designed to carry out the desired clustering. However, since
my version of matlab does not have the cell array feature, I leave the
last step to you.

% Choose a two-columned array A of pairs regarded as equivalent
A = [1 10;1 11;1 12;2 11;5 10;5 15;6 18];

% Then develop a transitive, commutative (symmetric) matrix M.
[B,ig,N] = unique(A(:)); % Find all the unique elements of A
n = size(B,1);
N = reshape(N,[],2); % N is same size as A with indices into B
M = accumarray(N,1,[n,n]); % Place 1's in M corres. to pairs in N
M = M+M.'; % Make M symmetric
M(M~=0) = 1; % Set nonzeros equal to 1
fg = true;
while fg % Loop until M is entirely transitive and symmetric
 Mc = M; % Save copy of M
 M = M*M+M; % Extend M transitively
 M(M~=0) = 1; % Replace nonzeros with ones
 fg = any(any(M~=Mc)); % Quit loop when M remains unchanged
end

% The unique rows of M indicate the desired equivalence sets (clusters)
C = unique(M,'rows'); % Find unique rows of M

% For each row of C, convert the 1's to the corresponding elements of A
c{i} = B(C(i,:)~=0);
% Presumably you will place each of these c{i}'s in a cell array, since
they are of differing lengths.

  The truth is that I don't have the 'unique' and 'accumarray' functions
either, so you should test this out carefully, since it is difficult for
me to do so thoroughly. I have only had the patience to carry out a
simple test where the 'unique' and 'accumarrays' have been manually
determined.

Roger Stafford

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics