简体   繁体   English

Matlab使用带有单元阵列的accumarray

[英]Matlab using accumarray with cell array

I am quite new to Matlab, but I have some experience with other programming languages. 我是Matlab的新手,但我对其他编程语言有一些经验。 I have a very large table imported from MySQL in Matlab. 我在Matlab中从MySQL导入了一个非常大的表。 It is given as cell array that looks something like this: 它作为单元格数组给出,如下所示:

 date   key     sales    weight  name
 12/11  101     1200     20      blue
 12/11  178     1200     70      purple
 13/11  209     1300     15      purple
 12/11  101     1200     10      blue
 14/11  678     1200     10      yellow
 12/11  340     1500     30      green
 17/11  178     1900     50      purple

And I want the output to be this: 我希望输出是这样的:

 key     sales    weight  name
 101     2400     30      blue
 178     3100     120     purple
 209     1300     15      purple
 678     1200     10      yellow
 340     1500     30      green

So I would like to combine the rows which have the same number in the column 'key'. 所以我想在'key'列中组合具有相同编号的行。 Meanwhile I would like to sum the column 'sales' and 'weight' and keep the column 'name' (each 'key' has the same 'name', but each 'name' can have multiple 'keys') 同时我想将“销售”和“重量”这一列加在一起,并保留“名称”栏目(每个“键”具有相同的“名称”,但每个“名称”可以有多个“键”)

I know this is possible with a for loop, but as I am having to manipulate a lot of tables in similar but different ways this is computational intensive. 我知道这可能是一个for循环,但由于我不得不以类似但不同的方式操作很多表,这是计算密集型的。

I have read in similar problems this can be solved using accumarray, but can this be done with accumarray with a sub as cell array? 我已经阅读了类似的问题,这可以使用accumarray来解决,但是这可以通过使用sub作为单元阵列的accumarray来完成吗? And how would that look like? 那会是怎样的样子?

Here is one method using accumarray , however it might be worth your while to consider the new table data structure instead of a cell matrix (I'm sure you can easily convert to it) 这是使用accumarray一种方法,但是考虑新的数据结构而不是单元格矩阵可能值得您accumarray (我确信您可以轻松转换为它)

T = {  101     1200     20      'blue'
       178     1200     70      'purple'
       209     1300     15      'purple'
       101     1200     10      'blue'
       678     1200     10      'yellow'
       340     1500     30      'green'
       178     1900     50      'purple'};

[u, ind, x] = unique([T{:,1}])

key = T(ind,1)
sales = accumarray(x', [T{:,2}])
weight = accumarray(x', [T{:,3}])
name = T(ind,4)

[key, num2cell(sales), num2cell(weight), name]
x={ '12/11'  101     1200     20      'blue'
 '12/11'  178     1200     70      'purple'
 '13/11'  209     1300     15      'purple'
 '12/11'  101     1200     10      'blue'
 '14/11'  678     1200     10      'yellow'
 '12/11'  340     1500     30      'green'
 '17/11'  178     1900     50      'purple'};

[~,b,c]=unique([x{:,2}]);
y=[x(b,2),...
    num2cell(sparse(1,c,[x{:,3}]))',...
    num2cell(sparse(1,c,[x{:,4}]))',...
    x(b,[5])];

unique is used to get the indices of duplicate keys. unique用于获取重复键的索引。 sparse is used to get the sum. sparse用于得到总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM