split up a matrix at discontinuities
15 views (last 30 days)
Show older comments
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
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
Accepted Answer
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)
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)
celldisp(result)
More Answers (1)
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':
See Also
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!