Added the above method to be able to specify different values for p_num_elemenets and dim1.