how to fit different lines for scatter points from specific categorical data?

2 views (last 30 days)
I got a table like this:
PLACE O18 D
Place3 -9.17 -71.68
Place2 -8.24 -61.47
Place2 -9.79 -70.24
Place3 -5.93 -62.67
Place2 -4.58 -29.3
Place1 -4.44 -26.5
Place1 -4.07 -28.4
...
which I imported like three column vectors: place, oxygen18, deuterium respectively.
plotted using
gscatter(oxygen18,deuterium,place)
and fitted a general tendency line
ft = fittype( 'poly1' );
[fit_line, gof] = fit( oxygen18, deuterium, ft);
now I would like fit a line 'oxygen18' vs 'deuterium' for each group entry in the vector 'place', i.e. 'oxigen18' vs 'deuterium' in Place1 and then for Place2 next for Place3, and so on.
How it could be achieved?.
Thanks.

Accepted Answer

Adam Danz
Adam Danz on 18 Sep 2019
Edited: Adam Danz on 23 Sep 2019
Easiest way is to do it in a loop.
placeUnq = unique(place); %all unique place values
fit_line_groups = cell(numel(placeUnq),1); %store results
gof_groups = fit_line_groups; %store results
% loop through each unique place
for i = 1:numel(placeUnq)
[fit_line_groups{i}, gof_groups{i}] = fit(oxygen18(place==placeUnq(i)), deuterium(place==placeUnq(i)), ft);
end

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!