my.binarize <- function (x, threshold = NA) 
{
    matd = x
    if (is.na(threshold)) {
        threshold = min(x) + (max(x) - min(x))/2
    }
    matd[matd <= threshold] = 0
    matd[matd > threshold] = 1
    return(list(matd=matd,threshold=threshold))
}
