how to use accumarray and polyfit

2 ビュー (過去 30 日間)
gianluca
gianluca 2015 年 1 月 7 日
コメント済み: gianluca 2015 年 1 月 7 日
hi, I would perform a simple linear fit on selected data in a large dataset to obtain the intercept and slope values. The data are of the form:
A = [100123 1 1 2500 50; 100123 1 2 2600 51; 100456 1 1 5000 120; 100456 1 2 5500 135; 100456 2 1 6000 150; 100456 2 2 6500 165];
I've to done different linear fit between the 4th (X-data) and the 5th (Y-data) columns depending on the 1st and 2nd columns (Keys and data series).
[key, ~, index] = unique(A(:, [1 2]), 'stable', 'rows');
mean = accumarray(index, A(:, 4), [], @mean); % that works!
fit = accumarray(indices, A(:, [4 5]), [], @poly); %Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
Is it possible to perform polyfit using the accumarray and handle function @poly ?

採用された回答

Guillaume
Guillaume 2015 年 1 月 7 日
No, you can't use accumarray on multiple columns. You would either have to handwrite a loop that does more or less the same as accumarray or you could do:
xdata = accumarray(indices, A(:, 4), [], @(v) {v});
ydata = accumarray(indices, A(:, 5), [], @(v) {v});
fit = cellfun(@(x, y) polyfit(x, y, 1), xdata, ydata, 'UniformOutput', false)
If you want fit as a matrix:
fit = cell2mat(fit)
  1 件のコメント
gianluca
gianluca 2015 年 1 月 7 日
Tnx Guillaume

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePreprocessing Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by