replaced vapply with matrix conversion

significant speedup by converting to a matrix rather than vapply