ourMELONS/R/cluster_own.R
Waldir Leoncio fca9caa731 Restyled files
Ran through styler::style_dir() in the R and tests directories in preparation for #23.
2021-11-10 14:25:50 +01:00

35 lines
1,007 B
R

cluster_own <- function(Z, nclust) {
true <- TRUE
false <- FALSE
maxclust <- nclust
# % Start of algorithm
m <- size(Z, 1) + 1
T <- zeros(m, 1)
# % maximum number of clusters based on inconsistency
if (m <= maxclust) {
T <- t((1:m))
} else if (maxclust == 1) {
T <- ones(m, 1)
} else {
clsnum <- 1
for (k in (m - maxclust + 1):(m - 1)) {
i <- Z[k, 1] # left tree
if (i <= m) { # original node, no leafs
T[i] <- clsnum
clsnum <- clsnum + 1
} else if (i < (2 * m - maxclust + 1)) { # created before cutoff, search down the tree
T <- clusternum(Z, T, i - m, clsnum)
clsnum <- clsnum + 1
}
i <- Z[k, 2] # right tree
if (i <= m) { # original node, no leafs
T[i] <- clsnum
clsnum <- clsnum + 1
} else if (i < (2 * m - maxclust + 1)) { # created before cutoff, search down the tree
T <- clusternum(Z, T, i - m, clsnum)
clsnum <- clsnum + 1
}
}
}
return(T)
}