Skip to content

Commit 7df8cec

Browse files
getMedianValue with option to exclude zeros
1 parent 36523a2 commit 7df8cec

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
*.swp
3+
tags

Library.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ requires("1.53h"); // minimum ImageJ version
4040

4141
// global, preset for all macros
4242
var delimiter = ":";
43-
var libraryVersion = "CU-MacroLibrary v1.00 (2023-04-27)";
43+
var libraryVersion = "CU-MacroLibrary v1.00 (2023-04-28)";
4444

4545
/*
4646
* Start
@@ -195,17 +195,21 @@ function getMedian(min_in, max_in)
195195
}
196196

197197
// Function to return the median value of an array
198-
function getMedianValue(array) {
198+
function getMedianValue(array, includeZeros) {
199199
output = NaN;
200+
if ( !includeZeros ) {
201+
array = Array.deleteValue(array, 0.0);
202+
}
200203
arrayLength = array.length;
204+
Array.sort(array); // in-place
201205
if ( arrayLength > 0 ) {
202206
if ( arrayLength == 1 ) { // single element (float or string)
203207
output = parseFloat(array[0]);
204208
} else if ( arrayLength % 2 != 0 ) { // odd number of elements
205209
output = array[Math.floor(arrayLength / 2)];
206210
} else { // even number of elements
207-
rightIndex = array[Math.floor(arrayLength / 2)];
208-
leftIndex = array[rightIndex - 1];
211+
rightIndex = Math.floor(arrayLength / 2);
212+
leftIndex = rightIndex - 1;
209213
output = (array[leftIndex] + array[rightIndex]) / 2.0;
210214
}
211215
}

0 commit comments

Comments
 (0)