Within splitapply, when min is invoked, it's invoked on a single group. Therefore, the index it returns refers to the index of the minimum within the group. It looks like it's the 2nd element of each group which is the minimum.
Using just one splitapply call, you won't be able to achieve what you want with an anonymous function, you'll need an explicit function:
function globalindex = locmingroup(values, indices)
[~, where] = min(abs(values - 2.2));
globalindex = indices(where);
end
Your splitapply call then becomes:
closestIndex = splitapply(@locmingroup, a(:, 2), (1:size(a, 1))', findgroups(a(:, 4)));
Note that if a(:, 4) is integers from 1 with no gap, as in your example, you don't need findgroups, you can just pass a(:, 4) directly.
Best Answer