[讨论] 第一列相同数值对应的第二列相加平均

楼主: ismyname (窝)   2017-09-18 12:18:57
怎么样把同一矩阵第一列的相同数值的找出来
然后对应的第二列相加平均阿
同一矩阵 两列
x= 1 1 10 5 17 1 18 2
4 3 2 1 8 8 4 5
怎么把 第一列相同 这个例子里是1提出
对应1的第二列 这个例子里分别是 4 3 8 三个数相加平均阿(平均出来为5)
变成x= 1 2 5 10 17 18
5 5 1 2 8 4
x=[1,1,10,5,17,1,18,2];
x=[x; 4 3 2 1 8 8 4 5];
x=sortrows(x',[1 2])'
x(1,:)
x(2,:)
figure
plot(x(1,:),x(2,:))
感谢
作者: LiamIssac (Madchester)   2017-09-18 13:06:00
idx = find(x(1,:) == a);y = mean(x(2, idx))
作者: sppmg (sppmg)   2017-09-18 13:19:00
假设重复只有一个且未知:[cc,ii]=sort(x(1,:))idx=find(! diff([cc(1),cc]))idx=ii(find(! diff([cc(1),cc]))) % 上面请改这个比较好x(2,idx) = mean( x(2,idx) )
作者: sunev (Veritas)   2017-09-18 14:18:00
用内建的accumarray或histcounts都可以A=accumarray(x(1,:)',x(2,:)',[],@mean);AA=find(A);[AA A(AA)]'histcounts好像不太行,那用accumarray就好了

Links booklink

Contact Us: admin [ a t ] ucptt.com