## find cycle in array

### Naime (view profile)

on 22 Oct 2018
Latest activity Edited by Sim

### Sim (view profile)

on 9 Oct 2019
I have a M M=[1,2 ;1,5; 2,4;2,5;4,5; 4,9;5,6;6,11;9,10;10,11] I want to find a loop in this array. I want this answer A={[1,2,5],[2,4,5],[4,5,6,9,10,11]}

Sim

### Sim (view profile)

on 7 Oct 2019
Hi Naime! I have your exact problem... Did you find any solution to share ?
Guillaume

### Guillaume (view profile)

on 8 Oct 2019
@simone, have you looked at the answers provided? In particular, I linked to two fileexchange submissions in my answer.
Sim

### Sim (view profile)

on 9 Oct 2019
Yes, thank you.. I tried what you suggested:
[1] Count all cycles in simple undirected graph version 1.2.0.0 (5.43 KB) by Jeff Howbert
[2] Count Loops in a Graph version 1.1.0.0 (167 KB) by Joseph Kirk
Unfortunately, both solutions [1] and [2] were not working for my case... but I understood more about the problem and the way to describe it! Thanks a lot, very helpful FileExchange submissions!
Just for information, the solution I was looking for was kindly provided by Matt J here:

### KSSV (view profile)

on 22 Oct 2018

M=[1,2 ;1,5; 2,4;2,5;4,5; 4,9;5,6;6,11;9,10;10,11] ;
A={[1,2,5],[2,4,5],[4,5,6,9,10,11]} ;
[c,ia,ib] = unique(M(:,1)) ;
N = length(c) ;
B = cell(N,1) ;
for i = 1:length(c)
T = M(ib==i,:) ;
B{i} = unique(T(:)) ;
end