I need to be able to find the index of the element within a 1d array that has the lowest non-negative value (so excluding all negative numbers). I know one way I finding it, namely using:
minVal = Inf;
index = 0;
for i in 1:length(arr)
if arr[i] >= 0 && arr[i] < minVal
minVal = arr[i];
index = i;
end
end
I wouldn’t suppose there’s some simpler, higher-performance way of doing this?
Unfortunately there is no method of argmin / argmax with a filter function as first element.
Maybe it would be good to add it?
Otherwise, I also could not see a “nicer” version as a loop. Performance-wise a loop is perfectly fine.
Allowing custom comparison operators in argmin (findmin) would allow a simple solution, since it could just use a comparison operator that always prefers non-negative values. This is WIP that seems stalled at the moment, cf