public static double mean(double[] data,int startIndex,int endIndex){
  double mean=0;
  int total=0;
  startIndex=Math.max(startIndex,0);
  startIndex=Math.min(startIndex,data.length - 1);
  endIndex=Math.max(endIndex,0);
  endIndex=Math.min(endIndex,data.length - 1);
  if (startIndex > endIndex)   startIndex=endIndex;
  for (int i=startIndex; i <= endIndex; i++) {
    if (Double.isNaN(data[i]))     throw new IllegalArgumentException("NaN not allowed in mean calculation");
    mean+=data[i];
    total++;
  }
  mean/=total;
  return mean;
}

public static double mean ( double [ ] data , int startIndex , int endIndex ) { double mean = 0 ; int total = 0 ; startIndex = Math . max ( startIndex , 0 ) ; startIndex = Math . min ( startIndex , data . length - 1 ) ; endIndex = Math . max ( endIndex , 0 ) ; endIndex = Math . min ( endIndex , data . length - 1 ) ; if ( startIndex > endIndex ) startIndex = endIndex ; for ( int i = startIndex ; i <= endIndex ; i ++ ) { if ( Double . isNaN ( data [ i ] ) ) throw new IllegalArgumentException ( "NaN not allowed in mean calculation" ) ; mean += data [ i ] ; total ++ ; } mean /= total ; return mean ; }
compute the mean of all elements in the array . no missing values ( nan ) are allowed .
