MATLAB: Is there a way to have GEOMEAN skip values which are NaN Statistics and Machine Learning Toolbox I would like to use GEOMEAN on my dataset, but it has some values that are NaN. I would like GEOMEAN to skip these values when it calculates the mean. Best Answer GEOMEAN cannot calculate the mean of a data set if it contains NaN values.The following example code demonstrates how to remove NaN values from a dataset:y = [ 1 NaN 2 3 4 NaN 5 6 7 NaN 8 9 10];s = isnan(y);y_mean = geomean(y(~s));The same solution holds for a matrix 'y':y = magic(4);y([3 13 1 2]) = NaN;s = isnan(y);y_mean = geomean(y(~s)); Related SolutionsMATLAB: How to eliminate Repeated NaNs in a array % Demo datax = [1:5 nan nan nan nan nan nan, 7:10, nan, 12, nan, 13:15, nan nan nan 19 ]; keepIdx = ~isnan(x); % keep all non-nans keepIdx(logical([1,diff(~keepIdx)])) = true; % keep first of consecutive nansx = x(keepIdx); % remove unwanted values from xy = y(keepIdx); % remove unwanted values from y MATLAB: Geomean on returns with negative values UseGR=100*(prod(1+R/100)^(1/numel(R))-1);where R is the rate of return for the period.Example:>> R=[9 1 2 3 -1]; % sample rate of returns, %>> GR=100*(prod(1+R/100)^(1/numel(R))-1) % geometric rate of returnGR = 2.7458 Related QuestionHow to replaces NaN’s with a corresponding vectorCorrelation between two row matricesHow to calculate a mean value of a vector and ignore from the “0” when appears inside the vectorsSame matrix but not equal problemHow to sort array matrix, finding means of output for matching months and reading stationsDoes a cell vector contains only NaN
Best Answer