📄 impplot.asv
字号:
% 'Om'=[] or 'Om'=0, means that orthogonal
% projections are requsted. (default)
% 'Om'=1 means that the factors are oblique.
% 'Om'=2 means that the ortho/oblique is solved automatically.
% This takes a little additional time.
% Xm : The model of X.
%
% Using the factors as they are (and the core, if defined) the general N-way model
% is calculated.
% $ Version 2.00 $ May 2001 $ Changed to array notation $ RB $ Not compiled $
% $ Version 1.02 $ Date 17. Apr 1999 $ Not compiled $
%
%
% Copyright
% Claus A. Andersson 1995-1999
% Chemometrics Group, Food Technology
% Department of Food and Dairy Science
% Royal Veterinary and Agricultutal University
% Rolighedsvej 30, T254
% DK-1958 Frederiksberg
% Denmark
% E-mail claus@andersson.dk
for i = 1:length(Factors);
DimX(i)=size(Factors{i},1);
end
i = find(DimX==0);
for j = 1:length(i)
DimX(i(j)) = size(G,i(j));
end
if nargin<2, %Must be PARAFAC
Fac=size(Factors{1},2);
G=[];
else
for f = 1:length(Factors)
if isempty(Factors{f})
Fac(f) = -1;
else
Fac(f) = size(Factors{f},2);
end;
end
end
if ~exist('Om')
Om=[];
end;
if isempty(Om)
Om=0;
end;
if size(Fac,2)==1,
Fac=Fac(1)*ones(1,size(DimX,2));
end;
N=size(Fac,2);
if size(DimX,2)>size(Fac,2),
Fac=Fac*ones(1,size(DimX,2));
end;
N=size(Fac,2);
Fac_orig=Fac;
i=find(Fac==-1);
if ~isempty(i)
Fac(i)=zeros(1,length(i));
Fac_ones(i)=ones(1,length(i));
end;
DimG=Fac;
i=find(DimG==0);
DimG(i)=DimX(i);
if isempty(G),
G=neye(DimG);
end;
G = reshape(G,size(G,1),prod(size(G))/size(G,1));
% reshape factors to old format
ff = [];
for f=1:length(Factors)
ff=[ff;Factors{f}(:)];
end
Factors = ff;
if DimG(1)~=size(G,1) | prod(DimG(2:N))~=size(G,2),
help nmodel
fprintf('nmodel.m : ERROR IN INPUT ARGUMENTS.\n');
fprintf(' Dimension mismatch between ''Fac'' and ''G''.\n\n');
fprintf('Check this : The dimensions of ''G'' must correspond to the dimensions of ''Fac''.\n');
fprintf(' If a PARAFAC model is established, use ''[]'' for G.\n\n');
fprintf(' Try to reproduce the error and request help at rb@kvl.dk\n');
return;
end;
if sum(DimX.*Fac) ~= length(Factors),
help nmodel
fprintf('nmodel.m : ERROR IN INPUT ARGUMENTS.\n');
fprintf(' Dimension mismatch between the number of elements in ''Factors'' and ''DimX'' and ''Fac''.\n\n');
fprintf('Check this : The dimensions of ''Factors'' must correspond to the dimensions of ''DimX'' and ''Fac''.\n');
fprintf(' You may be using results from different models, or\n');
fprintf(' You may have changed one or more elements in ''Fac'' or ''DimX'' after ''Factors'' have been calculated.\n\n');
fprintf(' Read the information above for information on arguments.\n');
return;
end;
FIdx0=cumsum([1 DimX(1:N-1).*Fac(1:N-1)]);
FIdx1=cumsum([DimX.*Fac]);
if Om==0,
Orthomode=1;
end;
if Om==1,
Orthomode=0;
end;
if Om==2,
Orthomode=1;
for c=1:N,
if Fac_orig(c)~=-1,
A=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
AA=A'*A;
ssAA=sum(sum(AA.^2));
ssdiagAA=sum(sum(diag(AA).^2));
if abs(ssAA-ssdiagAA) > 100*eps;
Orthomode=0;
end;
end;
end;
end;
if Orthomode==0,
Zmi=prod(abs(Fac_orig(2:N)));
Zmj=prod(DimX(2:N));
Zm=zeros(Zmi,Zmj);
DimXprodc0 = 1;
Facprodc0 = 1;
Zm(1:Facprodc0,1:DimXprodc0)=ones(Facprodc0,DimXprodc0);
for c=2:N,
if Fac_orig(c)~=-1,
A=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
DimXprodc1 = DimXprodc0*DimX(c);
Facprodc1 = Facprodc0*Fac(c);
Zm(1:Facprodc1,1:DimXprodc1)=ckron(A',Zm(1:Facprodc0,1:DimXprodc0));
DimXprodc0 = DimXprodc1;
Facprodc0 = Facprodc1;
end;
end;
if Fac_orig(1)~=-1,
A=reshape(Factors(FIdx0(1):FIdx1(1)),DimX(1),Fac(1));
Xm=A*G*Zm;
else
Xm=G*Zm;
end;
elseif Orthomode==1,
CurDimX=DimG;
Xm=G;
newi=CurDimX(2);
newj=prod(CurDimX)/CurDimX(2);
Xm=reshape(Xm',newi,newj);
for c=2:N,
if Fac_orig(c)~=-1,
A=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
Xm=A*Xm;
CurDimX(c)=DimX(c);
else
CurDimX(c)=DimX(c);
end;
if c~=N,
newi=CurDimX(c+1);
newj=prod(CurDimX)/CurDimX(c+1);
else,
newi=CurDimX(1);
newj=prod(CurDimX)/CurDimX(1);
end;
Xm=reshape(Xm',newi,newj);
end;
if Fac_orig(1)~=-1,
A=reshape(Factors(FIdx0(1):FIdx1(1)),DimX(1),Fac(1));
Xm=A*Xm;
end;
end;
Xm = reshape(Xm,DimX);
function G=neye(Fac);
% NEYE Produces a super-diagonal array
%
%function G=neye(Fac);
%
% $ Version 2.00 $ May 2001 $ Changed to array notation $ RB $ Not compiled $
% $ Version 1.00 $ Date 5. Aug. 1998 $ Not compiled $
%
% This algorithm requires access to:
% 'getindxn'
%
% See also:
% 'parafac' 'maxvar3' 'maxdia3'
%
% ---------------------------------------------------------
% Produces a super-diagonal array
% ---------------------------------------------------------
%
% G=neye(Fac);
%
% Fac : A row-vector describing the number of factors
% in each of the N modes. Fac must be a 1-by-N vector.
% Ex. [3 3 3] or [2 2 2 2]
% Copyright, 1998 -
% This M-file and the code in it belongs to the holder of the
% copyrights and is made public under the following constraints:
% It must not be changed or modified and code cannot be added.
% The file must be regarded as read-only. Furthermore, the
% code can not be made part of anything but the 'N-way Toolbox'.
% In case of doubt, contact the holder of the copyrights.
%
% Claus A. Andersson
% Chemometrics Group, Food Technology
% Department of Food and Dairy Science
% Royal Veterinary and Agricultutal University
% Rolighedsvej 30, DK-1958 Frederiksberg, Denmark
% E-mail claus@andersson.dk
N=size(Fac,2);
if N==1,
fprintf('Specify ''Fac'' as e vector to define the order of the core, e.g.,.\n')
fprintf('G=eyecore([2 2 2 2])\n')
end;
G=zeros(Fac(1),prod(Fac(2:N)));
for i=1:Fac(1),
[gi,gj]=getindxn(Fac,ones(1,N)*i);
G(gi,gj)=1;
end;
G = reshape(G,Fac);
function [i,j]=getindxn(R,Idx);
%GETINDXN
%
%[i,j]=GetIndxn(R,Idx)
%
% Copyright
% Claus A. Andersson 1995-
% Chemometrics Group, Food Technology
% Department of Food and Dairy Science
% Royal Veterinary and Agricultutal University
% Rolighedsvej 30, T254
% DK-1958 Frederiksberg
% Denmark
% E-mail: claus@andersson.dk
l=size(Idx,2);
i=Idx(1);
j=Idx(2);
if l==3,
j = j + R(2)*(Idx(3)-1);
else
for q = 3:l,
j = j + prod(R(2:(q-1)))*(Idx(q)-1);
end;
end;
function [MultPhi,Phis] = ncosine(factor1,factor2);
%NCOSINE multiple cosine/Tuckers congruence coefficient
%
% [MultPhi,Phis] = ncosine(factor1,factor2,DimX,Fac);
%
% ----------------------INPUT---------------------
%
% factor1 = cell array with loadings of one model
% factor2 = cell array with loadings of one (other) model
% If factor1 and factor2 are identical then
% the multiple cosine of a given solution is
% estimated; otherwise the similarity of the
% two different solutions is given
%
% ----------------------OUTPUT---------------------
%
% MultPhi Is the multiple cosine of the model
% Phis Is the cosine between components in
% individual component matrices arranged
% as [PhiA;PhiB ...]
% $ Version 2.00 $ May 2001 $ Changed to array notation $ RB $ Not compiled $
% $ Version 1.02 $ Date 28. July 1998 $ Not compiled $
%
% Copyright, 1998 -
% This M-file and the code in it belongs to the holder of the
% copyrights and is made public under the following constraints:
% It must not be changed or modified and code cannot be added.
% The file must be regarded as read-only. Furthermore, the
% code can not be made part of anything but the 'N-way Toolbox'.
% In case of doubt, contact the holder of the copyrights.
%
% Rasmus Bro
% Chemometrics Group, Food Technology
% Department of Food and Dairy Science
% Royal Veterinary and Agricultutal University
% Rolighedsvej 30, DK-1958 Frederiksberg, Denmark
% Phone +45 35283296
% Fax +45 35283245
% E-mail rb@kvl.dk
%
% Convert to old format
Fac = size(factor1,2);
for i = 1:length(factor1)
DimX(i) = size(factor1{i},1);
end
ff = [];
for f=1:length(factor1)
ff=[ff;factor1{f}(:)];
end
factor1 = ff;
ff = [];
for f=1:length(factor2)
ff=[ff;factor2{f}(:)];
end
factor2 = ff;
if length(factor1)~=length(factor2)
error(' factor1 and factor2 must hold components of same sizes in NCOSINE.M')
end
ord=length(DimX);
l_idx=0;
Fac=length(factor1)/sum(DimX);
for o=1:ord
l_idx=[l_idx sum(DimX(1:o))*Fac];
end
L1=reshape(factor1(1:DimX(1)*Fac),DimX(1),Fac);
L2=reshape(factor2(1:DimX(1)*Fac),DimX(1),Fac);
for f=1:Fac
L1(:,f)=L1(:,f)/norm(L1(:,f));
L2(:,f)=L2(:,f)/norm(L2(:,f));
end
%GT correction
Phis=L1'*L2;
%Previously: Phis=L2'*L2;
%End GT correction
MultPhi=Phis;
for i=2:ord
L1=reshape(factor1(l_idx(i)+1:l_idx(i+1)),DimX(i),Fac);
L2=reshape(factor2(l_idx(i)+1:l_idx(i+1)),DimX(i),Fac);
for f=1:Fac
L1(:,f)=L1(:,f)/norm(L1(:,f));
L2(:,f)=L2(:,f)/norm(L2(:,f));
end
phi=(L1'*L2);
MultPhi=MultPhi.*phi;
Phis=[Phis;phi];
end
function [b,All,MaxML]=ulsr(x,NonNeg);
%ULSR
%
% See also:
% 'unimodal' 'monreg' 'fastnnls'
%
% ------INPUT------
%
% x is the vector to be approximated
% NonNeg If NonNeg is one, nonnegativity is imposed
%
%
%
% ------OUTPUT-----
%
% b is the best ULSR vector
% All is containing in its i'th column the ULSRFIX solution for mode
% location at the i'th element. The ULSR solution given in All
% is found disregarding the i'th element and hence NOT optimal
% MaxML is the optimal (leftmost) mode location (i.e. position of maximum)
%
% Reference
% Bro and Sidiropoulos, "Journal of Chemometrics", 1998, 12, 223-247.
%
%
% [b,All,MaxML]=ulsr(x,NonNeg);
% This file uses MONREG.M
% $ Version 1.02 $ Date 28. July 1998 $ Not compiled $
%
% Copyright, 1998 -
% This M-file and the code in it belongs to the holder of the
% copyrights and is made public under the following constraints:
% It must not be changed or modified and code cannot be added.
% The file must be regarded as read-only. Furthermore, the
% code can not be made part of anything but the 'N-way Toolbox'.
% In case of doubt, contact the holder of the copyrights.
%
% Rasmus Bro & Nikos Sidiroupolos
% Chemometrics Group, Food Technology
% Department of Food and Dairy Science
% Royal Veterinary and Agricultutal University
% Rolighedsvej 30, DK-1958 Frederiksberg, Denmark
% Phone +45 35283296
% Fax +45 35283245
% E-mail rb@kvl.dk
%
x=x(:);
I=length(x);
xmin=min(x);
if xmin<0
x=x-xmin;
end
% THE SUBSEQUENT
% CALCULATES BEST BY TWO MONOTONIC REGRESSIONS
% B1(1:i,i) contains the monontonic increasing regr. on x(1:i)
[b1,out,B1]=monreg(x);
% BI is the opposite of B1. Hence BI(i:I,i) holds the monotonic
% decreasing regression on x(i:I)
[bI,out,BI]=monreg(flipud(x));
BI=flipud(fliplr(BI));
% Together B1 and BI can be concatenated to give the solution to
% problem ULSR for any modloc position AS long as we do not pay
% attention to the element of x at this position
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -