How to perform calculations on common elements of an array

1 回表示 (過去 30 日間)
harshpurohit11
harshpurohit11 2018 年 6 月 20 日
コメント済み: harshpurohit11 2018 年 6 月 21 日
I have A_names =[a,a,a,b,b,c,c,c,c]; B_values = [1,2,3,2,2,3,4,4,4]; length(A_names) = length(B_values). I need to find average of B_values for the common elements of A_names

採用された回答

Image Analyst
Image Analyst 2018 年 6 月 20 日
If you have the Statistics and Machine Learning Toolbox, use grpstats() - that's what it's meant for - to get statistics by group.
A_names = {'a';'a';'a';'b';'b';'c';'c';'c';'c'}
B_values = [1,2,3,2,2,3,4,4,4]
t = table(A_names, B_values')
output = grpstats(t, 1)
  1 件のコメント
harshpurohit11
harshpurohit11 2018 年 6 月 21 日
Thank you so much. This is exactly what I was looking for

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2018 年 6 月 20 日
You can loop through unique values of A_names. Here's a hint to get you started.
A_names ={'a','a','a','b','b','c','c','c','c'};
unqA = unique(A_names);
m = mean(B_values(strcmp(A_names,unqA{1})));

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by