Splitting an array by the value

4 ビュー (過去 30 日間)
Charlie Rannard
Charlie Rannard 2021 年 11 月 25 日
コメント済み: Charlie Rannard 2021 年 11 月 25 日
Say I have a set of values (points in the last 3 games): [ 9 9 6 4 4 7 9 9 9 9 9 9 9 7 7 5 7 7 9 9 9 9 9]
and also attendance in each of these games: [95.4508 85.2212 97.6083 99.3070 99.9607 99.5018 99.4400 97.4173 99.0898 99.8577 99.8764 99.5824 99.4344 99.5955 97.8574 99.8539 99.8427 99.8652 98.2601 99.7846 99.7284 103.4049 99.8820]
How could I break up the points array into sub-categories e.g 3pts, 6pts, 9pts to take an average attendance for that number of points to plot, or any other way i can plot this data. Thanks

採用された回答

Dyuman Joshi
Dyuman Joshi 2021 年 11 月 25 日
y = [9 9 6 4 4 7 9 9 9 9 9 9 9 7 7 5 7 7 9 9 9 9 9];
att = [95.4508 85.2212 97.6083 99.3070 99.9607 99.5018 99.4400 97.4173 99.0898 99.8577 99.8764 99.5824 99.4344 99.5955 97.8574 99.8539 99.8427 99.8652 98.2601 99.7846 99.7284 103.4049 99.8820];
att3pt = mean(att(find(y<=3))) %there are no points less than 3
att3pt = NaN
att6pt = mean(att(find(y>3&y<=6)))
att6pt = 99.1825
att9pt = mean(att(find(y>6&y<=9)))
att9pt = 98.5838
  1 件のコメント
Charlie Rannard
Charlie Rannard 2021 年 11 月 25 日
Great thanks!

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

その他の回答 (1 件)

Dave B
Dave B 2021 年 11 月 25 日
編集済み: Dave B 2021 年 11 月 25 日
You can do this really easily with groupsummary
x=[9 9 6 4 4 7 9 9 9 9 9 9 9 7 7 5 7 7 9 9 9 9 9];
y=[95.4508 85.2212 97.6083 99.3070 99.9607 99.5018 99.4400 97.4173 99.0898 99.8577 ...
99.8764 99.5824 99.4344 99.5955 97.8574 99.8539 99.8427 99.8652 98.2601 99.7846 ...
99.7284 103.4049 99.8820];
[mu,groups]=groupsummary(y',x','mean')
mu = 5×1
99.6338 99.8539 97.6083 99.3325 98.3164
groups = 5×1
4 5 6 7 9
bar(groups,mu)
Cool bonus of using groupsummary is that you can also grab other summary statistics, like the standard deviation:
figure
[stats,groups]=groupsummary(y',x',["mean" "std"]);
mu=stats(:,1);
bar(groups,mu);
hold on
sig = stats(:,2);
errorbar(groups,mu,sig,'LineStyle','none','CapSize',0,'Color','k','LineWidth',2)
  1 件のコメント
Charlie Rannard
Charlie Rannard 2021 年 11 月 25 日
Great, that will help a lot as the no. points can depend on user input. Thanks a lot.

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

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by