sum(j == I) is counting the number of entries in I that are equal to 1, 2, 3, ... 15322. The "if" around it is an implicit test for non-zero, so it is testing to see if there are any entries at all that match. A better way would be
I would suggest, though, that you would probably be better off using
[tf, idx] = ismember(x, I);
for j = idx ...
and probably you would be better off yet inverting the search, like ismember(I, x)
Best Answer