MATLAB: Meaning of ” if sum(j == I)”

sum

I have
I = 31×1 and x = 15322×1 and then it's written:
for j=1:length(x)-1
if sum(j == I)
What does sum(j == I) mean????

Best Answer

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
if any(j == I)
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)