请问如何实现二维数组第一列相同数字的行求平均。

比如下面a是2x7的原始数组,需要最后变成b,就是把第一列相同数字的行求平均
a=[1 2;1 4;2 3;2 5;3 4;3 7;3 1];
% b=[1 3;2 4;3 4];
谢谢各位大佬!

 採用された回答

hakaer
hakaer 2023 年 3 月 30 日

0 投票

a=[1 2;1 4;2 3;2 5;3 4;3 7;3 1];
%% 方法1
index =unique(a(:,1));
JG=[];
for i =1:length(index)
    JG=[JG;[index(i),mean(a(a(:,1)==index(i),2))]];
end
%% 方法2利用表的groupsummary 组汇总计算
T=array2table(a, 'VariableNames',{'a1','a2'});
G = groupsummary(T,'a1','mean' );
temp=table2array(G);
out=temp(:,[1,3]);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

質問済み:

2023 年 3 月 30 日

回答済み:

2023 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by