forked from andrewssobral/nway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monreg.m
70 lines (65 loc) · 1.63 KB
/
monreg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function [b,B,AllBs]=monreg(x);
%MONREG monotone regression
%
% See also:
% 'unimodal' 'monreg' 'fastnnls'
%
%
% MONTONE REGRESSION
% according to J. B. Kruskal 64
%
% b = min|x-b| subject to monotonic increase
% B = b, but condensed
% AllBs = All monotonic regressions, i.e. AllBs(1:i,i) is the
% monotonic regression of x(1:i)
%
% Reference
% Bro and Sidiropoulos, "Journal of Chemometrics", 1998, 12, 223-247.
%
% [b,B,AllBs]=monreg(x);
% Copyright (C) 1995-2006 Rasmus Bro & Claus Andersson
% Copenhagen University, DK-1958 Frederiksberg, Denmark, [email protected]
%
I=length(x);
if size(x,2)==2
B=x;
else
B=[x(:) ones(I,1)];
end
AllBs=zeros(I,I);
AllBs(1,1)=x(1);
i=1;
while i<size(B,1)
if B(i,1)>B(min(I,i+1),1)
summ=B(i,2)+B(i+1,2);
B=[B(1:i-1,:);[(B(i,1)*B(i,2)+B(i+1,1)*B(i+1,2))/(summ) summ];B(i+2:size(B,1),:)];
OK=1;
while OK
if B(i,1)<B(max(1,i-1),1)
summ=B(i,2)+B(i-1,2);
B=[B(1:i-2,:);[(B(i,1)*B(i,2)+B(i-1,1)*B(i-1,2))/(summ) summ];B(i+1:size(B,1),:)];
i=max(1,i-1);
else
OK=0;
end
end
bInterim=[];
for i2=1:i
bInterim=[bInterim;zeros(B(i2,2),1)+B(i2,1)];
end
No=sum(B(1:i,2));
AllBs(1:No,No)=bInterim;
else
i=i+1;
bInterim=[];
for i2=1:i
bInterim=[bInterim;zeros(B(i2,2),1)+B(i2,1)];
end
No=sum(B(1:i,2));
AllBs(1:No,No)=bInterim;
end
end
b=[];
for i=1:size(B,1)
b=[b;zeros(B(i,2),1)+B(i,1)];
end