how to use accumarray and polyfit

조회 수: 1 (최근 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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by