matlab version with variable lengths and init with medoid