How can I segment a matrix based on the difference between a column's elements of the matrix?

7 views (last 30 days)
I have a matrix (X), I want it to be segmented such that for each segment, the difference between successive first column entries does not exceed 6. The output should be as (xx) or call it whatever.
%%% The INPUT %%%
X=[
8.3700 -53.3090
11.4000 -116.6670
13.0000 -117.8350
26.7000 -105.8580
36.4000 -121.5060
39.4000 -116.0400
65.1000 -95.1370
65.9000 -123.0920
70.3000 -133.5950
122.0000 -113.7320
]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The OUTPUT %%%
xx=[
8.3700 -53.3090
11.4000 -116.6670
13.0000 -117.8350
]
xx=[
26.7000 -105.8580
]
xx=[
36.4000 -121.5060
39.4000 -116.0400
]
xx=[
65.1000 -95.1370
65.9000 -123.0920
70.3000 -133.5950
]
xx=[
122.0000 -113.7320
]

Accepted Answer

Torsten
Torsten on 18 Sep 2022
X=[
8.3700 -53.3090
11.4000 -116.6670
13.0000 -117.8350
26.7000 -105.8580
36.4000 -121.5060
39.4000 -116.0400
65.1000 -95.1370
65.9000 -123.0920
70.3000 -133.5950
122.0000 -113.7320
];
i = find(abs(diff(X(:,1)))>6);
i = [i(1);diff(i);size(X,1)-i(end)];
xx = mat2cell(X,i)
xx = 5×1 cell array
{3×2 double } {[26.7000 -105.8580]} {2×2 double } {3×2 double } {[ 122 -113.7320]}

More Answers (0)

Community Treasure Hunt

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

Start Hunting!