Loading...
Logo
Processing Forum
Hello,

(I'm still a beginner) I'm trying to think of an efficient way to calculate median values for a lot of data. I need to ignore zero values and only calculate the median of the non-zero values. I don't think I'm using sort correctly, nor does this seem like an efficient approach in general. I have 2 questions:

1. Why does this code return "[F@1fcabd4" in the print statement for each entry in median? 
2. Is there a more efficient way to do this overall?

Thanks!

Copy code
  1. void meanMedian(boolean leftRight) {
  2.   if (!leftRight) {
  3.     median = new float[totalSeconds + 86400]; 
  4.     for (int i = 0; i < totalDays; i ++) {
  5.       int skipped = 0; 
  6.       float [] edaToday = new float[86400];  
  7.       for (int p = 0; p < 86400; p++) { 
  8.         edaToday[p] = totalEda[i*86400 + p];
  9.         if (totalEda[i*86400 + p] == 0) {
  10.           skipped ++;
  11.         }
  12.       }
  13.       sort(edaToday);
  14.       reverse(edaToday);
  15.       sort(edaToday, 86400-skipped); 
  16.       float [] medianArray = new float[86400 - skipped]; 
  17.       medianArray = sort(edaToday, 86400-skipped);
  18.       median[i] = medianArray[86400/2]; 
  19.       println("median day " + i + " =  " + median); 
  20.     }
  21.   }
  22. }

Replies(2)


Why does this code return "[F@1fcabd4" in the print statement for each entry in median?

median is an array not an element in an array I think line 19 should be

Copy code
  1.       println("median day " + i + " =  " + median[i]); 

Rather than using an array for edaToday I would use an ArrayList and only copy non-zero values into it i.e.

Copy code
  1. void meanMedian(boolean leftRight) {
  2.   if (!leftRight) {
  3.     ArrayList<Float> edaToday = new ArrayList<Float>();
  4.     median = new float[totalSeconds + 86400]; 
  5.     for (int i = 0; i < totalDays; i ++) {
  6.       int skipped = 0; 
  7.       for (int p = 0; p < 86400; p++) { 
  8.         edaToday[p] = totalEda[i*86400 + p];
  9.         if (totalEda[i*86400 + p] != 0) { // Only consider non-zero values
  10.           edaToday.add(totalEda[i*86400 + p];
  11.         }
  12.       } // end for p loop
  13.       // At this point edaToday has all none zero values so get the median
  14.       Collections.sort(edaToday);
  15.       median[i] = edaToday.get(edaToday.size()/2);
  16.       println("median day " + i + " =  " + median[i]);
  17.     }  // end for i loop
  18.   }  // end of if statement
  19. } // end of function



Thanks! This is a huge help
Have a great day