how to use accumarray and polyfit

1 view (last 30 days)
gianluca
gianluca on 7 Jan 2015
Commented: gianluca on 7 Jan 2015
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 ?

Accepted Answer

Guillaume
Guillaume on 7 Jan 2015
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)

More Answers (0)

Categories

Find more on Preprocessing Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!