How to find the index of splitapply

4 ビュー (過去 30 日間)
Yaser Khojah
Yaser Khojah 2019 年 4 月 29 日
コメント済み: Yaser Khojah 2019 年 4 月 30 日
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.

採用された回答

Matt J
Matt J 2019 年 4 月 29 日
編集済み: Matt J 2019 年 4 月 29 日
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 件のコメント
Matt J
Matt J 2019 年 4 月 30 日
編集済み: Matt J 2019 年 4 月 30 日
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
Yaser Khojah
Yaser Khojah 2019 年 4 月 30 日
Dear Matt J thanks a lot for your help and that helps a lot :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by