I'm looking for a way to split my data into group then find the maximum and index of each group. So, I have this code which gives me the maximums but the indexes are not correct. Any idea please. I have asked this question and haven’t got the right answer yet. This is a simple example but in my big matrix there might be more than an answer. For example two max values. Is the code will select one only or will not work?
XX = [1 2 3 4 5 1 3]'; YY = [10 10 10 20 20 30 20]';[uv,~,idx] = unique(XX);[Result] = splitapply(@max, YY, idx);figurescatter(uv,Result)line(uv,Result)indices = splitapply(@maxIndex, YY, idx);function out=maxIndex(z) [~,out]=max(z);endm = [uv, Result, indices] 1 30 2 2 10 1 3 20 2 4 20 1 5 20 1 %The correct one should be
m = 1 30 62 10 23 20 84 20 45 20 5
If you like to test on my data, I have attached my data and code as below.
x = round(MaT_All(:,18),0);y = MaT_All(:,17);[uv,~,idx] = unique(x);[Result] = splitapply(@max, y, idx);figurescatter(uv,Result)line(uv,Result)indices = splitapply(@maxIndex, y, idx);function out=maxIndex(z) [~,out]=max(z);end
Best Answer