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