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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!