MATLAB uses linear indexing for arrays, however many arrays are also referred to by subscripts into the rows, columns, and additional dimensions. The ind2sub function converts the linear indices into subscripts.
In this example:
[r,c] = ind2sub(size(T), idx);
the ind2sub call takes linear index ‘idx’ and converts it to the appropriate subscripts into matrix ‘T’. (It appears to be completely appropriate here.)
See the documentation section on Matrix Indexing for an extended discussion.