Tidying up a list

Posted by Jonas on Stack Overflow See other posts from Stack Overflow or by Jonas
Published on 2010-03-16T14:18:24Z Indexed on 2010/03/16 14:51 UTC
Read the original article Hit count: 175

Filed under:

I'm fairly sure there should be an elegant solution to this (in Matlab), but I just can't think of it right now.

I have a list with [classIndex, start, end], and I want to collapse consecutive class indices into one group like so:

This

 1     1    40
 2    46    53
 2    55    55
 2    57    64
 2    67    67
 3    68    91
 1    94   107

Should turn into this

 1     1    40
 2    46    67
 3    68    91
 1    94   107

How do I do that?

EDIT

Never mind, I think I got it - it's almost like fmarc's solution, but gets the indices right

a=[  1     1    40
     2    46    53
     2    55    55
     2    57    64
     2    67    67
     3    68    91
     1    94   107];

d = diff(a(:,1));
startIdx = logical([1;d]);
endIdx   = logical([d;1]);
b = [a(startIdx,1),a(startIdx,2),a(endIdx,3)];

© Stack Overflow or respective owner

Related posts about matlab