Fill NAN values in a table

조회 수: 35 (최근 30일)
Sarah Yun
Sarah Yun 2019년 12월 15일
댓글: Image Analyst 2019년 12월 16일
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.

채택된 답변

Image Analyst
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
Sarah Yun
Sarah Yun 2019년 12월 15일
편집: Sarah Yun 2019년 12월 15일
This is great, thanks Image Analyst.
Question:
If I want to ignore NANs instead of interpolate (and put ignored values back in table), will this work - or maybe it will cause problems for future analysis?
Thank you.
Image Analyst
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개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by