Remove more than two consecutive NaNs and delete the same rows from the date column in a table
1 view (last 30 days)
Show older comments
Hello,
I am going through a bit complicated problem, as I have some in-situ data that needs to be cleaned, and one of the things I have to do is to delete more than two consecutive NaN values. I managed to do that by applying the code below.
The second problem that I faced is that to apply that code, I had to separate my data column from the table (contains date + data columns). And what I want is a way to include the date column, so when I delete the NaNs, the relative date rows should be gone too.
In the example below, I had to select the second column which represents the data, while the first column is the date.
Thank you
workData = readtable('station_2015.xlsx');
a = workData{:,2};
idx1 = isnan(a);
idx1 = idx1'
idr1 = diff(find([1 diff(idx1) 1]));
D1 = mat2cell(a,idr1,size(a',1));
for i = 1:length(D1)
if any(isnan(D1{i})) && length(D1{i})>2
D1{i} = [] ;
end
end
iwant1 = cell2mat(D1)
0 Comments
Accepted Answer
Voss
on 17 Jun 2022
% a table with 2 columns, containing some NaNs in the second column:
data = (1:20).';
data([2:3 7:9 13 15:19]) = NaN;
workData = table(rand(20,1),data,'VariableNames',{'Date','Data'});
disp(workData);
% use regexp to find sequences of consecutive NaNs of length at least 3:
[start_idx,end_idx] = regexp(char('0'+isnan(workData{:,2}).'),'1{3,}','start','end')
% get the indices of all elements within those length-3+ sequences of NaNs:
to_remove = arrayfun(@(s,e)colon(s,e),start_idx,end_idx,'UniformOutput',false);
to_remove = [to_remove{:}]
% remove the rows at those indices from the table:
workData(to_remove,:) = []
3 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!