Can I average specific elements on an array while ignoring others efficiently? For example, if have a text file like so:
Freq Force etc.
5 654 ....
5 754 ....
5 645 ....
10 1029 ....
10 1175 ....
10 1110 ....
15 1354 ....
15 1375 ....
15 1390 ....
Can I easily get the result:
Freq Force etc.
5 684 ....
10 1105 ....
15 1373 ....

 採用された回答

Stephen23
Stephen23 2017 年 8 月 25 日
編集済み: Stephen23 2017 年 8 月 25 日

0 投票

You could use dlmread and accumarray:
>> M = dlmread('temp3.txt','',1,0)
M =
5 654
5 754
5 645
10 1029
10 1175
10 1110
15 1354
15 1375
15 1390
>> [~,~,X] = unique(M(:,1));
>> A = accumarray(X,M(:,2),[],@mean)
A =
684.33
1104.67
1373.00
Tested on the file attached to this answer.

1 件のコメント

Euan Rodgers
Euan Rodgers 2017 年 8 月 25 日
編集済み: Euan Rodgers 2017 年 8 月 25 日
Thanks for your answer, works very well.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 8 月 25 日

0 投票

You can use dlmread() or importdata(), and then use grpstats() if you have the Statistics and Machine Learning Toolbox.

1 件のコメント

Stephen23
Stephen23 2017 年 8 月 25 日
編集済み: Stephen23 2017 年 8 月 25 日

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

カテゴリ

ヘルプ センター および File Exchange で Data Type Identification についてさらに検索

タグ

質問済み:

2017 年 8 月 25 日

編集済み:

2017 年 8 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by