Find zeros in a large table and replace with average of two previous column values
Show older comments
Hi everyone,
I have a fairly big table that I've imported from excel (11201x136). The first column is essentially a time series in seconds and the remaining 135 columns are different sets of data. For some columns, there is missing data which is pulling as NaN and this is fine as I don't need to use these columns. However, there are other columns where some of the data has not recorded properly and therefore some values are zero. What I am looking to do is find and replace those zero values across each column with an average of the two previous readings but I'm pretty new to matlab and struggling to write the correct code. I can't attach the data I'm afraid as it is confidential.
I've loaded the data as a table as the first column is hh mm ss which doesn't seem to work with xlsread (example below):
Data = readtable('Data.xls', 'sheet', 'Rawdata')
Correcting the zeros is the first thing I need to do but unfortunately it seems more difficult than I anticipated...
Any help is much appreciated,
Answers (1)
KSSV
on 26 Jan 2022
0 votes
You can find the zeros using logical indexing or the functon find. Replace those 0 values with NaN and then use the function fillmissing. Read about fillmissing.
5 Comments
Charlotte Bell
on 26 Jan 2022
Torsten
on 26 Jan 2022
idx = Data.Variables ==0
Data.Variables(idx) = NaN
Charlotte Bell
on 26 Jan 2022
Charlotte Bell
on 26 Jan 2022
Categories
Find more on Tables 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!