MATLAB Answers

0

How to find the index of splitapply

Asked by Yaser Khojah on 29 Apr 2019
Latest activity Commented on by Yaser Khojah on 30 Apr 2019
I have this code where I split the data into intervals and I look to find the maximum value for each interval as below. I need to find the indexes of the maximum values (Result) as well to collect more information from the original matrix (data). For simplify I created the following example.
x = round(Data(:,18),0);
y = Data(:,17);
[uv,~,idx] = unique(x);
[Result] = splitapply(@max, y, idx);
How can I find the indexes correspond to the Result to get more information from the original data.
Thank you so much for your help.

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Matt J
on 29 Apr 2019
Edited by Matt J
on 29 Apr 2019
 Accepted Answer

The indices within each group or the indices over all of y? If the former,
indices = splitapply(@maxIndex, y, idx);
function out=maxIndex(z)
[~,out]=max(z);
end

  7 Comments

Dear Matt,
I'm not really sure why I get these values for the indices? they are all ones as below although uv and Result are different numbers as below:
M = [uv Result indices]
0 -52.6375201767288 1
1 8.16048927046429 1
2 32.8959501232429 1
3 40.6235344557864 1
4 34.9060598363029 1
5 40.1999523692772 1
6 40.8218054895630 1
7 40.6603648293852 1
8 44.9101394298133 1
9 46.3799406552174 1
10 45.5282239893887 1
11 44.5752434568953 1
12 43.9269340744265 1
13 47.2730139688185 1
14 47.5348218152510 1
15 47.2414263217449 1
16 46.1472661513097 1
17 43.8672894544233 1
18 43.9554365031925 1
19 43.3286228656092 1
20 40.6979098991405 1
21 39.9119400060329 1
22 40.4076381192138 1
23 40.6247235584278 1
24 28.6474646180134 1
25 24.1315924439042 1
26 25.8657575418280 1
27 6.87298386234900 1
28 8.86434344246124 1
29 14.9836953775187 1
30 11.0986590169539 1
31 -13.2860150017084 1
32 5.28578052709843 1
33 8.46536624907806 1
34 2.08700412241725 1
35 2.33072961721340 1
36 5.64500984492813 1
40 -7.82948439124770 1
41 -20.4453841342906 1
If you try it on this example you will see the indices are not correct:
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);
figure
scatter(uv,Result)
line(uv,Result)
indices = splitapply(@maxIndex, YY, idx);
function out=maxIndex(z)
[~,out]=max(z);
end
m = [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 6
2 10 2
3 20 8
4 20 4
5 20 5
%any idea how to fix this?
Matt J
on 30 Apr 2019
That is why I asked you at the beginning if you meant, "The indices within each group or the indices over all of y?" If you meant the former, then your first set of results is correct. For example, if I extract all the YY for which idx==1, I obtain,
group=[10,30]
and clearly the maximum over the group occurs at the second element, so indices=2 is the correct output.
However, since It is now clear that you meant "the indices over all of y", here is the solution for that,
XX = [1 2 3 4 5 1 3].';
YY = [10 10 10 20 20 30 20].';
[uv,~,idx] = unique(XX,'stable');
II=(1:numel(XX)).';
S = splitapply(@(xy)maxIndex(xy), [II,YY], idx);
[Result,indices]=deal(S(:,1),S(:,2))
m = [uv, Result, indices]
function out=maxIndex(xy)
[ymax,loc]=max(xy(:,2),[],1);
out=[ymax,xy(loc,1)];
end
Dear Matt J thanks a lot for your help and that helps a lot :)

Sign in to comment.