mexed version of formHistogram and extension to 13-point weighted sampling