필터 지우기
필터 지우기

Fastest way to replace NaNs by empty cells in a table

조회 수: 25 (최근 30일)
Blue
Blue 2020년 6월 25일
댓글: Adam Danz 2020년 6월 30일
Hi,
I need to replace the NaNs in the columns a and b by empty cells. The code below works but it is pretty slow. Is there a faster way to do this ?
a = rand(1, 1000000)'; a(1:10000) = NaN;
b = rand(1, 1000000)'; b(1:10000) = NaN;
c = repelem({'A101'}, 1000000)';
t = table(a, b, c);
var_names = t.Properties.VariableNames;
for ii = 1:length(var_names)
if ~iscell(t.(var_names{ii}))
t.(var_names{ii}) = cellstr(num2str(t.(var_names{ii})));
idx = contains(t.(var_names{ii}), 'NaN');
t.(var_names{ii})(idx) = {''};
end
end
Thank you,
  댓글 수: 1
Adam Danz
Adam Danz 2020년 6월 30일
I urge you not to do this. The table variables in columns 1 and 2 are numeric and NaN values indicate missing values. To replace the NaN values with emtpy cells, all of your numeric values will have to converted to cells. That not only looks ugly but it makes the table more difficult to work with.
If you're writing the table to a file and do not want NaN values printed, there are other ways around that.
Converting the numeric values to cells just so the NaN values can appear as empty will likely cause more problems than it aesthetically solves.

댓글을 달려면 로그인하십시오.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 6월 25일
M=10;N=5;
a = rand(1, M)'; a(1:N) = NaN;
b = rand(1, M)'; b(1:N) = NaN;
c = repelem({'A101'}, M)';
t = table(a, b, c);
t.a=num2cell(t.a);
t.a(cellfun(@isnan,t.a))={''}

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by