split up a matrix at discontinuities

16 views (last 30 days)
I have matricies of varying sizes, how can i split them where the time is discontinous? Time is in seconds with 60 seconds in between each interval, otherwise it is considered "discontinous"
Example:
Here the data set is "discontinous" between 720 and 53138, how can I split them along with their paired Y axis without having to look at where the discontinuity is and manaually creating the different matricies?
  1 Comment
AH
AH on 22 Apr 2024
The criterion for splitting the data is discontinuity. So I don't think that it's possible to split the data into pieces without specifying locations where discontinuity occurs.
Here is one simple code
t = [0:60:720 53138:60:53618 101413:60:101713]';
y = 2*t;
% Find discontinuity
dt = diff(t);
disIdx = find(abs(dt) > 60);
N = length(t);
disIdx = [disIdx(:);N]; % add last segment
Ndis = length(disIdx);
% Split data
Y = cell(Ndis,1);
beginIdx = 1;
for i = 1:Ndis
idx = beginIdx:min(N,disIdx(i));
Y{i} = [t(idx),y(idx)];
beginIdx = disIdx(i)+1;
end
Y
Y = 3x1 cell array
{13x2 double} { 9x2 double} { 6x2 double}

Sign in to comment.

Accepted Answer

Voss
Voss on 22 Apr 2024
A = [0 34.4; 60 33.3; 120 32.2; 180 31.1; 240 30; 300 28.9; 360 27.9; 420 26.8; 480 25.7; 540 24.7; 600 23.6; 660 22.6; 720 21.6; 53138 68.7; 53198 69.9; 53258 71.1; 53318 72.2; 53378 73.4; 53438 74.5; 53498 75.5; 53558 76.5; 53618 77.4];
disp(A)
1.0e+04 * 0 0.0034 0.0060 0.0033 0.0120 0.0032 0.0180 0.0031 0.0240 0.0030 0.0300 0.0029 0.0360 0.0028 0.0420 0.0027 0.0480 0.0026 0.0540 0.0025 0.0600 0.0024 0.0660 0.0023 0.0720 0.0022 5.3138 0.0069 5.3198 0.0070 5.3258 0.0071 5.3318 0.0072 5.3378 0.0073 5.3438 0.0075 5.3498 0.0076 5.3558 0.0076 5.3618 0.0077
idx = find(diff(A(:,1)) ~= 60);
s_idx = [1; idx+1];
e_idx = [idx; size(A,1)];
result = arrayfun(@(s,e)A(s:e,:),s_idx,e_idx,'UniformOutput',false)
result = 2x1 cell array
{13x2 double} { 9x2 double}
celldisp(result)
result{1} = 0 34.4000 60.0000 33.3000 120.0000 32.2000 180.0000 31.1000 240.0000 30.0000 300.0000 28.9000 360.0000 27.9000 420.0000 26.8000 480.0000 25.7000 540.0000 24.7000 600.0000 23.6000 660.0000 22.6000 720.0000 21.6000 result{2} = 1.0e+04 * 5.3138 0.0069 5.3198 0.0070 5.3258 0.0071 5.3318 0.0072 5.3378 0.0073 5.3438 0.0075 5.3498 0.0076 5.3558 0.0076 5.3618 0.0077

More Answers (1)

Lokesh
Lokesh on 22 Apr 2024
Hi puccapearl,
I understand that you are looking to split the matrix into smaller matrices(or segments) whenever there is a discontinuity in time greater than 60 seconds between successive entries.
Here are the steps you can follow to identify discontinuities and segment them accordingly:
  • Calculate Time Differences: Compute the difference between successive time points to identify where the discontinuities (gaps greater than 60 seconds) occur.
  • Identify Discontinuities: Use the calculated differences to find indices in the matrix where these discontinuities happen.
  • Split the Matrix: Based on the identified indices, split the matrix into segments, each representing a continuous time series without discontinuities greater than 60 seconds.
Below is a sample MATLAB code snippet that demonstrates how to split the matrix based on discontinuity:
% Define the matrix
data = [
0, 1.4;
60, 1.2;
120, 1.3;
180, 1.3;
720, 1.3;
780, 1.5;
840, 1.23
];
% Calculate the differences between successive time points
timeDiffs = diff(data(:,1));
% Identify where the time difference is greater than 60 seconds
discontinuities = find(timeDiffs > 60);
% Initialize a cell array to hold the continuous segments
segments = {};
% The starting index for the first segment
startIndex = 1;
% Iterate through each discontinuity to split the matrix
for i = 1:length(discontinuities)
% The discontinuity index marks the end of the current segment
endIndex = discontinuities(i);
% Extract the segment and add it to the segments array
segments{end+1} = data(startIndex:endIndex, :);
% Update the start index for the next segment
startIndex = endIndex + 1;
end
% Add the last segment from the last discontinuity to the end of the data
segments{end+1} = data(startIndex:end, :);
% Each cell of 'segments' now contains a continuous part of the original matrix
Refer to the following MATLAB documentation to know more about 'diff' and 'find':

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!