ourMELONS/R/cluster_own.R

36 lines
925 B
R
Raw Normal View History

2020-11-09 15:22:10 +01:00
cluster_own <- function(Z, nclust) {
2020-11-19 08:22:38 +01:00
true <- TRUE
false <- FALSE
2020-11-09 15:22:10 +01:00
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)) {
2021-05-20 09:18:05 +02:00
i = Z[k, 1] # left tree
2020-11-09 15:22:10 +01:00
if (i <= m) { # original node, no leafs
2020-11-19 07:53:36 +01:00
T[i] = clsnum
2020-11-09 15:22:10 +01:00
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
}
2021-05-20 09:18:05 +02:00
i <- Z[k, 2] # right tree
2020-11-09 15:22:10 +01:00
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)
}