replace any nan value in the table by zero

I have a 34420x229 table I want to replace any nan value in the table by zero. how can I do so ?

回答 (2 件)

Robert Hamilton
Robert Hamilton 2016 年 5 月 16 日
編集済み: Robert Hamilton 2016 年 5 月 16 日

6 投票

I've spent the better part of the day looking for an answer to this, and I've finally found some success with this format (so I figured I better share):
T{:,:}(isnan(T{:,:})) = 0
If all the columns are not numeric, the above will return an error. However, can use standard MATLAB range notations, e.g. T{:,10:end}(isnan(T{:,10:end})) = 0 to just search and replace in from the 10th column to the end of the table.
I do wish that ismissing() and standardizeMissing() would allow you the choice of what you wanted to put into the table for the missing value. In my case, it is histogram data pulled from a database, and if the measuring device didn't record data in a given bin, it doesn't bother to send a 0 to save on transmission costs. So, I needed to replace all the missing data -- the NaNs -- with 0s.

2 件のコメント

Richard Vergin
Richard Vergin 2018 年 4 月 28 日
Thanks a lot this worked perfectly!
Peter Perkins
Peter Perkins 2018 年 4 月 30 日
In more recent versions of MATLAB, fillmissing would be the thing to use.

サインインしてコメントする。

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 2 日

0 投票

M(isnan(M))=0

10 件のコメント

wesso Dadoyan
wesso Dadoyan 2016 年 5 月 2 日
it doesn't work. I am receiving:Undefined function 'isnan' for input arguments of type 'table'.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 2 日
If b is your table
x=table2array(b)
x(isnan(x))=0;
b{:,:}=x
wesso Dadoyan
wesso Dadoyan 2016 年 5 月 2 日
Also it doesn't work Error using table2array (line 27) Cannot concatenate the table variables 'vD' and 'vI', because their types are char and cell.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 2 日
Can you post an example?
wesso Dadoyan
wesso Dadoyan 2016 年 5 月 2 日
Dear Azzi,
What do you need exactly since the matrix is huge in size?
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 16 日
You don't need to post all your Matrix, just a sample of your data, It could be a 5x5 matrix or cell array, I don't know what you mean by table.
Image Analyst
Image Analyst 2016 年 5 月 16 日
"table" is a new data type that was introduced in R2013b. It's like a cell array but has a lot less overhead and memory requirements. It's limited to cases where the entire column is one data type, though different columns can be different types. Like column 1 is all double scalars, and column 2 is all strings.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 16 日
Sometimes, it's not what they mean by table! that's why I asked to post an example
Gabor
Gabor 2021 年 3 月 11 日
Undefined function 'isnan' for input arguments of type 'table'
Steven Lord
Steven Lord 2021 年 3 月 11 日
That's correct, the isnan function is not defined for table arrays. I recommend using fillmissing instead, which can handle both numeric and tabular arrays.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

タグ

質問済み:

2016 年 5 月 2 日

コメント済み:

2021 年 3 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by