How to specify a default fill value for adding to table

조회 수: 50 (최근 30일)
Greg
Greg 2016년 10월 31일
편집: Anna Iatckova 2022년 9월 15일
I have a table and want to add data to it iteratively. My data is sparse. Most of it is doubles. When I add a column with not all rows specified, the unspecified rows are filled in with 0, which is an unfortunately valid number. It would be better if they had NaN by default. How can I change this default table fill parameter? I currently loop through the newly added columns and look for blocks of zeros immediately after adding, but this is failing in the more complex data manipulations I'm doing.

답변 (3개)

Peter Perkins
Peter Perkins 2016년 11월 2일
Greg, it's really the double type that's doing the in-filling with zeros, not the table. In general, things in a table behave the same way they would if they were directly in the workspace.
I imagine you're doing an assignment something like
t.NewVar(someRows) = values
You might be better off "preallocating" with NaN before your assignment, with something like
t.NewVar = NaN(height(t),1)
t.(someRows) = values
Hope this helps.

Kevin Gleason
Kevin Gleason 2016년 11월 2일
Perhaps you are mixing data types in these arrays?
Default values should be as follows: 0 for durations, NaN for numbers, NaT for dates, undefined for categorical arrays etc. I am not aware of any way to customize these defaults.
Regardless, there are many functions that can help you clean up or fix missing data. Refer to the following documentation page to see some of them in action:
  댓글 수: 1
Greg
Greg 2017년 2월 20일
I am mixing data types, which is why I am using a table, and why this is complicated.
All of the functions I've found to handle missing data require that I be able to identify it. But some of my data is validly 0, and some of it is missing-data 0, so there's no way to use these functions.

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


Anna Iatckova
Anna Iatckova 2022년 9월 15일
편집: Anna Iatckova 2022년 9월 15일

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by