总计的

version 1.1.0.1 (10.5 KB) by Kelly Kearney
Group (and apply functions to) values in one matrix based on grouping variables in another

522 Downloads

更新13 Jul 2022

来自Github

View License on GitHub

骨料: aggregate values in a matrix

作者:凯利·科尔尼(Kelly Kearney)

This repository includes the code for the骨料总计的hist.mMATLAB功能以及运行它们所需的所有因功能。

该函数基于x中的类别值组合y的值。它或多或少地像Accumaray和更新的SplitApply功能一样,但具有更灵活的选项,用于分组变量。

内容

  • 入门
  • Syntax
  • 例子
  • Contributions

入门

Prerequisites

This function requires Matlab R14 or later.

下载和安装

This code can be downloaded fromGithub或者Matlabcentral文件交换. The File Exchange entry is updated daily from the GitHub repository.

Matlab Search Path

需要将以下文件夹添加到MATLAB搜索路径(通过addpath,pathtool, etc.):

总计的-PKG/总计的

Syntax

[XCON,YAGG,YIDXAGG] =骨料(x,y)[XCON,YAGG,YIDXAGG] =骨架(x,x,y,fun)

输入变量:

  • x: n x p array, aggregator variable can be either numeric or a cell array of strings
  • y:n x m数组,要分组的值
  • 乐趣: function handle. If included, this function is applied to the grouped values of y

Output variables:

  • xcon: unique rows of x
  • yagg:与每个x相对应的y值的单元格数。
  • yidxagg:聚合值的行索引

例子

For this example, we'll find the maximum value in each month in the following dataset:

%样本数据nt=1000;t=datenum(2014,6,1)+rand(nt,1)*365;dv=datevec(t);y=rand(nt,5);

要使用Accumarray按月汇总,您首先需要将唯一的月份转换为分组索引,然后对y的每一列独立重复累积计算。您还需要指定输出大小,并添加一个函数以获取所有值,而不是平均值:

tic;[[unqdv1,~,idx]=unique(dv(:,1:2),'');nmonth=size(unqdv1,1);ncol=size(y,2);ymonthly1=zeros(nmonth,ncol);forii=1:ncolymonthly1(:,ii)=accumarray(idx,y(:,ii), [nmonth1], @(x)最大限度(x,[],,1);endTOCunqdv1ymonthly1
Elapsed time is 0.004129 seconds. unqdv1 = 2014 6 2014 7 2014 8 2014 9 2014 10 2014 11 2014 12 2015 1 2015 2 2015 3 2015 4 2015 5 ymonthly1 = 0.9828 0.99894 0.99039 0.99836 0.99153 0.97251 0.98854 0.99534 0.99817 0.99373 0.99326 0.98096 0.99718 0.97635 0.99907 0.98464 0.99274 0.99434 0.99898 0.98916 0.99374 0.98904 0.99184 0.98563 0.9946 0.96326 0.94601 0.99774 0.98785 0.9928 0.98269 0.98128 0.96038 0.99827 0.97032 0.99108 0.99053 0.99389 0.99406 0.99808 0.98049 0.97057 0.99275 0.97529 0.9952 0.98287 0.97395 0.99563 0.99642 0.98981 0.99646 0.99582 0.99945 0.96349 0.99475 0.97861 0.98167 0.99644 0.9871 0.98205

The aggregate function simplifies the syntax by removing the need for the aggregator (the first input to eitheraccumarray或者总计的)成为积极的整数。它还立即对y的所有列执行聚合,都简化了语法并加快计算(相对于循环和重复累积,如上所述)。

tic;[[unqdv2,ymonthly2]=总计的(dv(:,1:2),y, @(x)最大限度(x,[],,1);ymonthly2=(1, ymonthly2{:});TOCunqdv2ymonthly2
Elapsed time is 0.002709 seconds. unqdv2 = 2014 6 2014 7 2014 8 2014 9 2014 10 2014 11 2014 12 2015 1 2015 2 2015 3 2015 4 2015 5 ymonthly2 = 0.9828 0.99894 0.99039 0.99836 0.99153 0.97251 0.98854 0.99534 0.99817 0.99373 0.99326 0.98096 0.99718 0.97635 0.99907 0.98464 0.99274 0.99434 0.99898 0.98916 0.99374 0.98904 0.99184 0.98563 0.9946 0.96326 0.94601 0.99774 0.98785 0.9928 0.98269 0.98128 0.96038 0.99827 0.97032 0.99108 0.99053 0.99389 0.99406 0.99808 0.98049 0.97057 0.99275 0.97529 0.9952 0.98287 0.97395 0.99563 0.99642 0.98981 0.99646 0.99582 0.99945 0.96349 0.99475 0.97861 0.98167 0.99644 0.9871 0.98205

Contributions

欢迎社区对此包裹的贡献!

To report bugs, please submit anissue在github上,包括:

  • your operating system
  • 您的MATLAB版本和所有相关工具箱(类型ver在MATLAB命令行中获取此信息)
  • 代码/数据以重现错误或错误行为,以及收到的任何错误消息的全文

Please also feel free to submit enhancement requests, or to send pull requests (via GitHub) for bug fixes or new features.

我确实在注释中提出的任何问题监视Matlabcentral FileExchange条目,但希望在GitHub上跟踪问题。

发布ed with MATLAB R2019a

Cite As

Kelly Kearney (2022).总计的(https://github.com/kakearney/aggregate-pkg), GitHub. Retrieved.

MATLAB释放兼容性
Created with R2013a
与任何版本兼容
Platform Compatibility
视窗 苹果系统 Linux

Community Treasure Hunt

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

开始狩猎!

总计的

要查看或报告此GitHub附加组件中的问题,请访问GitHub Repository.
要查看或报告此GitHub附加组件中的问题,请访问GitHub Repository.