grouping first (left column) and last (right column) of consecutive sequence of a Nx2 matrix

1 view (last 30 days)
Hi there
We have a Nx2 matrix with pieces of consecutive values that we want to obtain the extreme left and right limits:
A=[100 200; 101 201; 102 202; 300 400; 801 901; 802 902]
we would like to obtain a matrix with limits (left column and right column) in the detected consecutive sequences of the first column. The isolated sequences (in this case [300 400] should be also retrieved.
for A the result would be;
B=[100 202; 300 400; 801 902] ;
thanks in advance

Accepted Answer

Guillaume on 24 Jan 2019
You haven't clearly defined what consecutive values means mathematically. I'm assuming a difference of 1. Also you haven't explained if both columns are to be considered independently (and what happen if values are consecutive in one column and not the others). I'm assuming that both columns have the exact same pattern so we can ignore column 2 for finding consecutive values.
A=[100 200; 101 201; 102 202; 300 400; 801 901; 802 902];
%finding start and end of consecutive runs. Only using 1st column for that
transitionrows = find(diff([-Inf; A(:, 1); +Inf]) ~= 1); %not consecutive when difference is not 1
startseqrows = transitionrows(1:end-1); %start row of consecutive run
endseqrows = transitionrows(2:end) - 1; %end row of consecutive run
B = [A(startseqrows, 1), A(endseqrows, 2)]

More Answers (1)

Paramonte on 24 Jan 2019
Many thanks it worked perfectely!!
Best Regards

Community Treasure Hunt

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

Start Hunting!