Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.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中使用accumarray求和数据_Matlab_Matrix_Accumarray - Fatal编程技术网

在Matlab中使用accumarray求和数据

在Matlab中使用accumarray求和数据,matlab,matrix,accumarray,Matlab,Matrix,Accumarray,我有这样一个矩阵: >>D=[1,0,10;3,1,12;3,1,12.5;6,1,6;6,2,11.1;] D = 1.0000 0 10.0000 3.0000 1.0000 12.0000 3.0000 1.0000 12.5000 6.0000 1.0000 6.0000 6.0000 2.0000 11.1000 如果第一列数据相同,我想得到第二列数据的总和。例如,我想要: E= 1.0000

我有这样一个矩阵:

>>D=[1,0,10;3,1,12;3,1,12.5;6,1,6;6,2,11.1;]
D =

1.0000         0   10.0000
3.0000    1.0000   12.0000
3.0000    1.0000   12.5000
6.0000    1.0000    6.0000
6.0000    2.0000   11.1000
如果第一列数据相同,我想得到第二列数据的总和。例如,我想要:

E=
1.0000         0
3.0000    2.0000
6.0000    3.0000
所以我试过了

b = accumarray(D(:,1),D(:,2),[],[],[],true);
[i,~,v] = find(b);
E = [i,v]

但它不起作用。我该怎么办?

以这种方式结合使用
unique
accumarray
-

[unique_ids,~,idmatch_indx] = unique(D(:,1)); 
%// unique_ids would have the unique numbers from first column and only 
%// used to get the first column of final output, E. 
%// idmatch_indx are tags put on each element corresponding to each unique_ids
%// based on the uniqueness

%// Accumulate and perform summation of elements from second column of D using
%// subscripts from idmatch_indx
E = [unique_ids accumarray(idmatch_indx,D(:,2))]
使用
accumarray
时,通常需要输入要在累积元素上使用的函数,但是
@sum
是默认函数句柄,在这种情况下,您可以忽略它