grouping non-zero entries in a matrix according to a rule

2 views (last 30 days)
I have a matrix say, a = [ 0 1 0 0 0 1 0; 0 0 0 0 0 1 1; 1 0 0 0 0 0 0; 0 1 0 0 0 0 0; 0 0 1 0 1 0 0; 0 0 0 1 0 0 1;]
I need to enumerate the non-zero elements into groups according to the following: - if two elements share a row or a column they are in the same group, and if say (1,2) & (1,6) in group1, then group1 will also include (2,6) & (2,7) & (6,7) & (4,2) & (6,4) I have been stuck with this for a while, any help is deeply appreciated.

Accepted Answer

Matt J
Matt J on 14 Apr 2014
Edited: Matt J on 14 Apr 2014
How about this
[I,J]=find(a);
C=bsxfun(@eq,I,I.') | bsxfun(@eq,J,J.');
The matrix C is a graph connectivity map which you can then use to segment into groups using this FEX file,
  2 Comments
Eman
Eman on 14 Apr 2014
Edited: Eman on 14 Apr 2014
thanks a lot Matt J, seems to be very promising, I can not relate through the original index of the element to the labels produced by the connected graph function, would you know? it is obvious that they index going through columns for non zeros elements.
Matt J
Matt J on 14 Apr 2014
Edited: Matt J on 14 Apr 2014
If the graph function says that m and n are connected, it means that (I(m),J(m)) is connected to (I(n),J(n)) where I and J were the output of
[I,J]=find(a);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!