Your advice help me find the exact location of error, which is really life-saving!
In my case, I created a boolean indexer like the following snippet, and I forgot to check the case that the indexer can be all False!
valid = distmat <= 10
tp = pred[valid, :]
blabla