To be sure: Would this loop solve your problem?
XX = [1 2 3 4 5 1 3]';
YY = [10 10 10 20 20 30 20]';
uXX = unique(XX);
Result = zeros(size(uXX));
Index = zeros(size(uXX));
for k = 1:numel(uXX)
match = find(XX == uXX(k));
[Result(k), idx] = max(YY(match));
Index(k) = match(idx);
end
If so, try if this is faster:
XX = [1 2 3 4 5 1 3]';
YY = [10 10 10 20 20 30 20]';
[uv, ~, idx] = unique(XX);
[Result, Index] = splitapply(@maxIndex, [YY, (1:numel(YY)).'], idx)
function [value, index] = maxIndex(z)
[value, k] = max(z(:, 1));
index = z(k, 2);
end
Here the index related to the YY vector is appended as 2nd column, such that the related information is available inside maxIndex also.
Use timeit or tic/toc to compare the runtime with providing 2 separate arrays:
[uv, ~, idx] = unique(XX);
[Result, Index] = splitapply(@maxIndex, YY, (1:numel(YY)).', idx)
function [value, index] = maxIndex(Y, IndexY)
[value, k] = max(Y);
index = IndexY(k);
end
Best Answer