List cleaning

I'm sure there should be an elegant solution for this (in MATLAB), but I just can't think about it right now.

I have a list with [classIndex, start, end], and I want to collapse consecutive indexes of the class into one group as follows:

it

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

Gotta turn into this

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

How to do it?

EDIT

Nothing, I think I got it - it's almost like fmarc solution , but it gets indexes correctly

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)];
+3
source share
2 answers

Here is one solution:

Ad = find([1; diff(A(:,1))]~=0);
output = A(Ad,:);
output(:,3) = A([Ad(2:end)-1; Ad(end)],3);
clear Ad
+2
source

One way to do this if the column in question is numeric: Build the differences on the id column. Successive identical elements will have zero here:

diffind = diff(a(:,1)');

, , .

b = a([true [diffind~=0]],:);

, , .

+1

Source: https://habr.com/ru/post/1737159/


All Articles