Solved by KFrank in post #2 
              
              
                Hi ljmzlh! 

The core issue is that feature[i][belong[i]==j] has a different 
shape for different values of i and j.  Therefore if you try to build a 
“tensor” whose “slices” are given by the above expression, you will 
end up with a “ragged tensor” (that is, a tensor whose slices have 
differing sh…