#!usr/bin/env Rscript
# Supporting function for aans.m
#
# based on code from Sohan Seth and Manuel Eugster in classic_aa.R

set.seed(1)

library("archetypes")
library(R.matlab)
inp <- readMat('classic_aa_Input.mat')
source('LstepArchetypesMod.R')
source('LFunctions_to_calculate_real_archetypes_with_swap2.R')
source('LstepArchetypoids.R')

# matFeatSam available in the workspace (from Matlab)
# data("toy")
# matFeatSam <- t(toy)

as <- stepLArchetypoids3(t(inp$matFeatSam), k=inp$nLat, norep = 10)
#a <- bestModel(as)

#a <-  stepArchetypoids(inp$nLat[1],nearest="cand_ns",t(inp$matFeatSam),as)


s <- list(matSamLat = as[[1]][[1]],  # numbers with the cases
          matLatSam = c(),  # H
          obj = as[[1]][[2]]
            )

writeMat('ada3_Output.mat',matSamLat = t(s$matSamLat), matLatSam = s$matLatSam, obj = s$obj)
save(as, file = "bestModel.RData")
