Fill NAN values in a table
35 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a table that consists of 5 columns
One column, temperature, displays some NAN values
I would like to use the inpaint_nans function to fill the NAN by interpolation
How do I fill in the temperature column only?
Then save the table as new table after NAN filled?
Thank you.
0 个评论
采纳的回答
Image Analyst
2019-12-15
Try this well commented example I created for you:
% Create data because the user forgot to post any!
% Make random values for Date, temp, humidity, pressure, precip, depth
numRows = 50;
temperature = randi(100, [numRows, 1]);
humidity = randi(100, [numRows, 1]);
pressure = randi(100, [numRows, 1]);
precip = randi(100, [numRows, 1]);
depth = randi(100, [numRows, 1]);
% Make some of the temperature elements nan
temperature(10:20) = nan;
indexes = randperm(length(temperature), 8); % 8 other random locations.
temperature(indexes) = nan;
% Create a table, T, as a container for the workspace variables.
T = table(temperature, humidity, pressure, precip, depth)
%------------------------------------------------------------------------
% % Now we have our table and we can begin.
% Now fix the nans. First extract the temperature column.
temperature = T.temperature
% Find nan locations (rows)
nanRows = isnan(temperature)
% Sometimes, either the first or last element(s) are nan, and the interpolation will leave a nan there.
% So to prevent that we have to find the first non-nan element and tack it onto the beginning,
% and find the last non-nan element and tack it on to the end.
indexes = find(~nanRows)
repairedTemperature = [temperature(indexes(1)); temperature; temperature(indexes(end))]; % Make sure to use semicolon rather than comma.
% Find nan locations (rows) again with the "fixed" array.
nanRows = isnan(repairedTemperature)
% Interpolate non-nan locations
repairedTemperature = interp1(find(~nanRows), repairedTemperature(~nanRows), 1:length(repairedTemperature))
% Now truncate off the first and last elements we tacked on. Also need to transpose since interp1() gives a row vector.
repairedTemperature = repairedTemperature(2:end-1)';
% Stick the results back into the table.
T.temperature = repairedTemperature;
fprintf('All done with demo by Image Analyst.\n');
2 个评论
Image Analyst
2019-12-16
I don't know. If you want to extract all non-nan rows, you can do this:
temperature = T.temperature
% Find nan locations (rows)
nanRows = isnan(temperature)
temperature = temperature(~nanRows);
Whether that shortened list causes problems is something you would know, not me. Depends on how you're going to use them. There is no need to put the "ignored" values back in the table since they never left the table. If you want a whole table without the nan rows, I think you can do this
T = T(~nanRows, :); % Remove rows where temperature is nan from the table.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!