covert NaN to zeros
Afficher commentaires plus anciens
“These will contain the value ‘NaN’ when imported. You should replace these with a value of zero.“ For a given set of data, it contains NaN in some where, how to pick then up and assign a zero to then as stated as the statement?
1 commentaire
dpb
le 11 Mai 2022
x(isnan(x))=0;
the most basic of logical indexing.
Réponses (3)
John fredson
le 12 Mai 2022
2 commentaires
What type of variable is x? Is it double, like d in the example below? Is it a table array, like t? Is it a cell array like c? Or is it some other type?
d = 1:10;
t = array2table(magic(4));
c = {'apple', 'banana'};
whos
If it's either double or table, I'd use fillmissing instead of assignment with isnan. isnan is not defined for table arrays but fillmissing can fill missing data inside a table.
d(5) = NaN
d2 = fillmissing(d, 'Constant', 0)
t{2, 3} = NaN
t2 = fillmissing(t, 'Constant', 0)
John fredson
le 15 Mai 2022
John fredson
le 12 Mai 2022
0 votes
1 commentaire
Steven Lord
le 12 Mai 2022
This doesn't seem to be related to the original question of how to replace NaN values with 0, so you should ask this as a new question (with more details about how the data is organized in your struct.) Use the Ask link just below the blue "MATLAB Answers" bar at the top of the page.
You say you have "text data".
Not sure what you really have, but look at this:
textData = 'abcdef';
textData(3) = nan
textData(isnan(textData)) = 0
textData(3)
textData(3) = '0'
Does that do anything like what you want and expect? If not, attach your variable in a .mat file with the paperclip icon after reading this:
2 commentaires
John fredson
le 15 Mai 2022
Walter Roberson
le 15 Mai 2022
fillmissing() with constant value 0
Catégories
En savoir plus sur Data Preprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
