Find maximum values related to index

1 回表示 (過去 30 日間)
mark cejas
mark cejas 2018 年 8 月 1 日
コメント済み: jonas 2018 年 8 月 1 日
Hello community,
I am trying to write a for loop to find the maximum values in column 2 for each group variable in column 1. I tried a for loop to no avail. Any help would be appreciated.
The solution should result in a maximum value, corresponding row index, for each group variable in column 1. So, the solution should yield 1) the group index associated with stests(:,1) 2) maximum values of stests(:,2) per group index 3) the row index of stests corresponding to each max value result
I attached a 2418x2 matrix called stests.
I apologize ahead of time for my lack of proper data science language.
Very much appreciated.
Regards, Mark

採用された回答

jonas
jonas 2018 年 8 月 1 日
編集済み: jonas 2018 年 8 月 1 日
Ideal job for findgroups and splitapply.
VAL is the max value in each group, located at id_l (local group index). For example, id_l=2 if max value is second value in group.
data=xlsread('stests.xlsx');
%%Find max value and local index
G=findgroups(data(:,1));
[VAL,id_l]=splitapply(@max,data(:,2),G);
%%Find global index
[~,b]=unique(data(:,1));
id_g=[id_l+b]-1;
%Store results
out=[VAL,id_g]
  2 件のコメント
mark cejas
mark cejas 2018 年 8 月 1 日
Thank you Jonas. I appreciate this elegant solution and the intro to the great function. Haven’t checked yet, but it looks great!!!
jonas
jonas 2018 年 8 月 1 日
You're welcome, let me know if you encounter some bug.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by