Umar
Umar

Reputation: 89

multiple automated curve fitting

I have a 'Matrix' (4x4) as following.

Matrix =

  206.3088    9.4042    1.2780    0.9845
  206.3099    4.6309    1.2050    0.9857
  206.3559    9.4029    1.3192    1.0236
  206.3573    4.6307    1.2421    1.0252

Now I need to do a multiple automated curve fitting between 'column 2' and 'column 3' data points. For a fitting procedure only adjacent values based on 'column 1' (time) should be selected. Also take an average for corresponding data in 'column 1' and 'column 4'.

For example in the given Matrix two curve fitting result can be achieved as following manually. Similarly, manually the corresponding average value for data points in 'column 1' and 'column 4' can be achieved.

Fit1=regstats(Matrix(1:2,3),Matrix(1:2,2),'linear','beta')
Fit2=regstats(Matrix(3:4,3),Matrix(3:4,2),'linear','beta')
C1 = mean (Matrix(1:2,1)) 
C2 = mean (Matrix(1:2,4))

The output should be like following for the 'Matrix'

Output =

  206.3093   Fit1.beta(1)  Fit1.beta(2)  0.9851
  206.3566   Fit2.beta(1)  Fit1.beta(2)  1.0244

Upvotes: 0

Views: 101

Answers (1)

v.tralala
v.tralala

Reputation: 1715

Assuming the criterion for neighbouring data points is that the difference of values in column 1 is less then 0.01 you can write the following:

clear; close all;
Matrix = [206.3088    9.4042    1.2780    0.9845;
    206.3099    4.6309    1.2050    0.9857;
    206.3559    9.4029    1.3192    1.0236;
    206.3573    4.6307    1.2421    1.0252];


groupsDelim = [0; find(diff(Matrix(:,1))>0.01); size(Matrix,1)];
%groupDelim are the indices of rows which mark the end of a group
%thus the start of a group is groupsDelim(ii)+1

Fits = cell(numel(groupsDelim)-1,1);
Cs = zeros(numel(groupsDelim)-1,2);
Output = zeros(numel(groupsDelim)-1,4);
for ii = 1:numel(groupsDelim)-1
    groupRows = (groupsDelim(ii)+1):groupsDelim(ii+1);
    Fits{ii}=regstats(Matrix(groupRows,3),Matrix(groupRows,2),'linear','beta');
    Cs(ii,1) = mean (Matrix(groupRows,1));
    Cs(ii,2) = mean (Matrix(groupRows,4));
    Output(ii,:) = [Cs(ii,1), Fits{ii}.beta', Cs(ii,2)];
end

Upvotes: 1

Related Questions