split up a matrix at discontinuities
Mostrar comentarios más antiguos
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 comentario
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
Respuesta aceptada
Más respuestas (1)
Lokesh
el 22 de Abr. de 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':
1 comentario
puccapearl
el 22 de Abr. de 2024
Categorías
Más información sobre Linear Algebra en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!