Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在matlab中从表生成pivot表_Matlab_Pivot Table - Fatal编程技术网

如何在matlab中从表生成pivot表

如何在matlab中从表生成pivot表,matlab,pivot-table,Matlab,Pivot Table,有没有一种简单的方法可以从matlab表在matlab中创建透视表? 在Excel中,或者Python中的pandas.pivot\u表中?我在文件交换上找到了,但它不适用于表 下面是一个示例,如果我有一个表t: name value _____ _____ 'Foo' 0 'Bar' -1 'Bar' 5 'Foo' 1 我想使用@sum函数在name列上进行聚合,以获得: name sum_of_value __

有没有一种简单的方法可以从matlab
在matlab中创建透视表? 在Excel中,或者Python中的pandas.pivot\u表中?我在文件交换上找到了,但它不适用于

下面是一个示例,如果我有一个表
t

name     value
_____    _____

'Foo'     0   
'Bar'    -1   
'Bar'     5   
'Foo'     1   
我想使用
@sum
函数在
name
列上进行聚合,以获得:

name     sum_of_value
_____    ________

'Bar'    4       
'Foo'    1   

有简单的方法吗?

我找到了一种使用accumarray的方法(可能有更好的方法):


编辑:我将其扩展为一个函数

您可以使用
grpstats
创建简单的汇总表。 但是,请使用
unstack
获取更复杂的轴

% Example: Create columns from Var1, summing the values for each date:

T = 
    date        item      value
    ________    _______   _______
    2015.2      a         1
    2015.2      a         1
    2015.2      b         1
    2015.2      c         1
    2015.4      a         2
    2015.4      b         2
    2015.4      c         2
    2015.4      d         2
    2016.2      a         3
    2016.2      b         3
    2016.2      c         3
    2016.2      d         3


T2 = unstack(T, 'value', 'item', 'GroupingVariables', 'date', 'AggregationFunction', @sum);

T2 = 
    date        a        b        c        d   
    ________    _____    _____    _____    _____
    2015.2      2        1        1        NaN
    2015.4      2        2        2        2
    2016.2      3        3        3        3
% Example: Create columns from Var1, summing the values for each date:

T = 
    date        item      value
    ________    _______   _______
    2015.2      a         1
    2015.2      a         1
    2015.2      b         1
    2015.2      c         1
    2015.4      a         2
    2015.4      b         2
    2015.4      c         2
    2015.4      d         2
    2016.2      a         3
    2016.2      b         3
    2016.2      c         3
    2016.2      d         3


T2 = unstack(T, 'value', 'item', 'GroupingVariables', 'date', 'AggregationFunction', @sum);

T2 = 
    date        a        b        c        d   
    ________    _____    _____    _____    _____
    2015.2      2        1        1        NaN
    2015.4      2        2        2        2
    2016.2      3        3        3        3