フィルターのクリア

Is there a better way to compute metrics on labeled array elements.

2 ビュー (過去 30 日間)
Burke Rosen
Burke Rosen 2018 年 6 月 17 日
編集済み: Burke Rosen 2018 年 6 月 18 日
For example, I have a 1d double array 'data' and a 1d cell array of strings called 'labels'. For each unique label I want the mean of the data. The best I have come up with is below. I don't believe this is fully vectorized. Is there a better way?
%%make sample dataset
n = 1000;
data = rand(n,1);
labels = char(randsample(97:122,n,true)');%[a-z]
%%get means for each label
[uniLab,~,labIdx] = unique(labels,'stable');% stable for speed
mu = arrayfun(@(x) mean(data(labIdx==x)),1:numel(uniLab));

採用された回答

Walter Roberson
Walter Roberson 2018 年 6 月 17 日
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 17 日
The last step of your code can be replaced by
accumarray(labIdx, data, [], @mean)
Burke Rosen
Burke Rosen 2018 年 6 月 18 日
編集済み: Burke Rosen 2018 年 6 月 18 日
This yields a ~25% speed increase at n = 1e3 and ~5% at n = 1e5. (500 trials per algorithm, randomized order). Thank you.

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

その他の回答 (1 件)

Burke Rosen
Burke Rosen 2018 年 6 月 17 日
Thank you for that tip @Walter.
After further review:
1. The way I wrote the sample data set, labels is actually a character array not a cell array, one has to cellstr it to yield that.
2. mu = grpstats(data,labels,'mean') is compact, easy to read, and maybe 1 or 2 percent faster that my formulation, if one adds the cellstr.
3. My solutions is 5x faster than grpstats if labels is a character rather than a cell array.
4. My guess is that unique operates much faster on character arrays than cell arrays and the runtime of the loop (or arrayfun) over the unique labels is negligible compared the unique itself.

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by