replace nan with a value
Afficher commentaires plus anciens
I have a column in which some values are missing and shown as NaN. I need to replace all these NaN with the median of the column. Here is what I tried: I am extracting column 4 in which there are missing values, find the median value, Use isnan to replace the logical 1 with the median.
Column 4 values: 1 2 3 4 5 6 NaN 12 10 NaN 4 5 NaN
C=num(:,4);
median=nanmedian(C);
R(isnan(C))=median;
But with this I am getting results as : 0 0 0 0 0 0 12 0 0 12 0 0 12
But the expected result is: 1 2 3 4 5 6 12 12 10 12 4 5 12
Any suggestions ?
Réponse acceptée
Plus de réponses (1)
Steven Lord
le 21 Juin 2017
z = [1 2 3 4 5 6 NaN 12 10 NaN 4 5 NaN].';
y = fillmissing(z, 'constant', median(z, 'omitnan'));
showBothColumnsSideBySide = [z, y]
3 commentaires
Daphne Mariaravi
le 21 Juin 2017
Jan
le 21 Juin 2017
@Daphne: This happens, because "median" was defined as a variable on your computer. Do not shadow builtin functions by variables. Solution: Restart Matlab or:
clear median
Daphne Mariaravi
le 21 Juin 2017
Catégories
En savoir plus sur Structures 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!