for segment, index in zip(segment_ids, indices):
      xgrad[index] += ygrad[segment] * weights[segment]