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

1 次查看(过去 30 天)
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.

采纳的回答

Adam Danz
Adam Danz 2019-9-18
编辑:Adam Danz 2019-9-23
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by