Delete Entries in Cell arrays with a certain criterea

1 Ansicht (letzte 30 Tage)
Maximilian comploj
Maximilian comploj am 21 Okt. 2016
Beantwortet: Nick Counts am 5 Nov. 2016
Hello I Have a cell :
A= [2011 12 3 8 52 36] [2019]
[2011 12 3 8 52 40] [2004]
[2011 12 3 8 52 52] [2004]
[2011 12 3 14 50 50] [2015]
the first row contains a Matrix with the date in yyyy mm dd HH MM SS
i need to delete the double entry for 2004 delete second or third row if the date is less distant than one day Thanks a lot

Antworten (1)

Nick Counts
Nick Counts am 5 Nov. 2016
This is messy, but here is an example. I'm sure there's a slick way to do this with logical indexing, but I can't quite wrap my head around it.
Sometimes a for-loop will get it done when slicker methods are elusive. I tried to comment the example, so hopefully it makes sense to you. I also added an additional row with [2004] in column 7 that is more than 1 day away, and it is correctly left alone.
A = [[2011 12 3 8 52 36] [2019];
[2011 12 3 8 52 40] [2004];
[2011 12 5 8 52 52] [2004];
[2011 12 3 14 50 50] [2015];
[2011 12 3 8 52 52] [2004]]
disp('Starting with these dates:')
datestr(datenum(deal(A(:,1:6))))
% Find unique values in column 7
uniqueValues = unique(A(:,7));
% how often does each unique value from column 7 occur?
freq = hist(A(:,7), uniqueValues);
% find index of column 7 values that appear more than once
nonUniqueInd = ismember(A(:,7),uniqueValues(freq>1));
% Make a list of values that are repeated in column 7
nonUniqueVals = A(nonUniqueInd,7)
% we will be building an index of rows to delete
indToRemove = [];
% Loop through each value that is repeated. Will run on the same
% value more than once to caputure the case when a column 7 value
% occurs multiple times on multiple days
for i = 1:numel(nonUniqueVals)
% try each non-unique value from column 7
% get the index of each occurrence of the current non-unique
% value in the original array
nuInd = find(A(:,7)==nonUniqueVals(i))
% find elapsed time from first occurrence of the repeated value
% in column 7
deltas = datenum(deal(A(nuInd(1),1:6)))-(datenum(deal(A(nuInd,1:6))))
% find any time changes that are 0 or greater than 1. This
% gives us the first occurrence and anthing on a different day
thisIndToRemove = nuInd(find( (deltas == 0 | deltas > 1)))
% Add indices to the running list. We could delete them here,
% but it's a little better to build a small array of indices then
% to keep resizing the original array, which could potential be
% very large.
indToRemove = vertcat(indToRemove, thisIndToRemove);
end
% Remove duplicates matching our conditions
A(indToRemove,:) = [];
disp('Here are the remaining rows');
datestr(datenum(deal(A(:,1:6))))

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by