⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tourgui.m

📁 常用ROBUST STATISTICAL
💻 M
📖 第 1 页 / 共 2 页
字号:
if colflag == 1
    % Then plot using colors based on ud.classlab
    ud = get(0,'userdata');
end
% Now generate a y for each observation
%
for i=1:n     %loop over each observation
    for j=1:length(theta)
        y(i,j)=data(i,:)*ang(j,:)'; 
    end
end
% Display the curve
if colflag == 0
    % then display without class colors
    for i=1:n
        set(Hline(i),'xdata',theta,'ydata',y(i,:));
    end
elseif colflag == 1
    % then display WITH group colors.
    % Can only display up to 7 groups by color. this should be sufficient.
    % We will use the default MATLAB colors
    cols = {'b';'g';'r';'c';'m';'y';'k'};
    clab = unique(ud.classlab);
    if length(clab) > length(cols)
        errordlg('The maximum number of allowed groups is 7.')
        return
    end
    for k = 1:length(clab)
        % loop over all of the different colors and display
        inds = find(ud.classlab==clab(k));
        for i=inds
            set(Hline(i),'xdata',theta,'ydata',y(i,:),'color',cols{k});
        end  
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function P = permweg(p)
% This gets a smaller number of permutations to get all possible ones.

N = ceil((p-1)/2);
% Get the first sequence.
P(1) = 1;
for k = 1:(p-1)
    tmp(k) = (P(k) + (-1)^(k+1)*k);
    P(k+1) = mod(tmp(k),p);
end
% To match our definition of 'mod':
P(find(P==0)) = p;

for j = 1:N;
    P(j+1,:) = mod(P(j,:)+1,p);
    ind = find(P(j+1,:)==0);
    P(j+1,ind) = p;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function runptour(x,maxit,Hf,delt)

% PSEUDOTOUR    Pseudo Grand Tour
% special version for the GUI.

gtud = get(Hf,'userdata');      % here we will store the iteration and the step

% Now do the tour stuff
[n,p] = size(x);
if rem(p,2) ~= 0
    % Add zeros to the end.
    x = [x,zeros(n,1)];
    p = p+1;
end
% Set up vector of frequencies.
th = mod(exp(1:p),1);
% This is a small irrational number:
% delt = exp(1)^(-5); 
cof = sqrt(2/p);
% Set up storage space for projection vectors.
a = zeros(p,1);
b = zeros(p,1);
z = zeros(n,2);

if isempty(gtud)
    % then get the initial plot stuff.
    % Get an initial plot, so the tour can be implemented 
    % using Handle Graphics.
    gtud.ph = plot(z(:,1),z(:,2),'o','erasemode','normal');
    gtud.maxit = maxit;
    gtud.Hf = Hf;
    gtud.delt = delt;
    gtud.iter = 1;
    gtud.t = 0;
    axis equal, axis off
    set(Hf,'backingstore','off','renderer','painters','DoubleBuffer','on')
    gtud.H = uicontrol(gcf,'style','text',...
        'units','normalized',...
        'position',[0.01 0.01 0.2 0.05],...
        'horizontalalignment','left',...
        'string','Iteration: ');
    Hbuttstop = uicontrol(Hf,'style','pushbutton',...
        'units','normalized',...
        'position',[0.9 0.01, 0.1 0.05],...
        'string','Stop Tour',...
        'createfcn','ud=get(0,''userdata'');ud.pgtstop=0;set(0,''userdata'',ud)',...
        'deletefcn','ud=get(0,''userdata'');ud.pgtstop=1;set(0,''userdata'',ud)',...
        'callback','ud=get(0,''userdata'');ud.pgtstop=1;set(0,''userdata'',ud)');
    Hbuttstart = uicontrol(Hf,'style','pushbutton',...
        'units','normalized',...
        'position',[0.65 0.01, 0.15 0.05],...
        'string','Continue Tour',...
        'callback',['ud=get(0,''userdata'');',...
            'ud.pgtstop=0;set(0,''userdata'',ud);',...
            'tourgui(''pseudotour'')']);
    set(Hf,'userdata',gtud)
end
pause(0.01)
% When user pushes stop button, stoptour is reset to true - stop the tour
% when user pushes the restart, stoptour is reset to false - run the tour

iter = gtud.iter;
t = gtud.t;
while iter <= maxit
    t = t + delt;
    gtud.t = t;
    ud = get(0,'userdata');
    if ud.pgtstop == 0
        % tour is not stopped
        % Find the transformation vectors.
        for j=1:p/2
            a(2*(j-1)+1)=cof*sin(th(j)*t);
            a(2*j)=cof*cos(th(j)*t);
            b(2*(j-1)+1)=cof*cos(th(j)*t);
            b(2*j)=cof*(-sin(th(j)*t));
        end
        % Project onto the vectors.
        z(:,1)=x*a;
        z(:,2)=x*b;
        set(gtud.ph,'xdata',z(:,1),'ydata',z(:,2))
        set(gtud.H,'string',['Iteration: ',int2str(iter)])
        drawnow
    else
        % give the user a chance to bring up the tourgui and save data
        % or they just might want to look at it. 
        ud.pgt = [a(:), b(:)];
        set(0,'userdata',ud)
        break
    end
    iter = iter + 1;
    gtud.iter=iter;
    set(Hf,'userdata',gtud)
end


function runtorustour(x,maxit,Hf,delt)

% TORUSTOUR    Grand Tour - Torus Winding Algorithm
% Special version for the GUI

gtud = get(Hf,'userdata');  % here we store the iteration and the step
[n,p] = size(x);
% Set up vector of frequencies.
N = 2*p - 3;
lam = mod(exp(1:N),1);
% This is a small irrational number:
% delt = exp(1)^(-5); 
% Get the indices to build the rotations.
J = 2:p;
I = ones(1,length(J));
I = [I, 2*ones(1,length(J)-1)];
J = [J, 3:p];
E = eye(p,2);   % Basis vectors
if isempty(gtud)
    % Get an initial plot.
    z = zeros(n,2);
    gtud.ph = plot(z(:,1),z(:,2),'o','erasemode','normal');
    axis equal, axis off
    set(Hf,'backingstore','off','renderer','painters','DoubleBuffer','on')
    gtud.maxit = maxit;
    gtud.Hf = Hf;
    gtud.delt = delt;
    gtud.k = 1;     % this is the iteration index
    gtud.gtdisp = 1;    % since this is a scatterplot tour
    gtud.H = uicontrol(gcf,'style','text',...
        'units','normalized',...
        'position',[0.01 0.01 0.2 0.05],...
        'horizontalalignment','left',...
        'string','Iteration: ');
    Hbuttstop = uicontrol(Hf,'style','pushbutton',...
        'units','normalized',...
        'position',[0.9 0.01, 0.1 0.05],...
        'string','Stop Tour',...
        'createfcn','ud=get(0,''userdata'');ud.gtstop=0;set(0,''userdata'',ud)',...
        'deletefcn','ud=get(0,''userdata'');ud.gtstop=1;set(0,''userdata'',ud)',...
        'callback','ud=get(0,''userdata'');ud.gtstop=1;set(0,''userdata'',ud)');
    Hbuttstart = uicontrol(Hf,'style','pushbutton',...
        'units','normalized',...
        'position',[0.65 0.01, 0.15 0.05],...
        'string','Resume Tour',...
        'callback',['ud=get(0,''userdata'');',...
            'ud.gtstop=0;set(0,''userdata'',ud);',...
            'tourgui(''grandtour'')']);
    set(Hf,'userdata',gtud)
    drawnow
end
pause(0.01)
k = gtud.k;
% Start the tour.
while k <= maxit
    ud = get(0,'userdata');
    if ud.gtstop == 0
        % tour is not stopped
        % Find the rotation matrix.
        Q = eye(p);
        for j = 1:N
            dum = eye(p);
            dum([I(j),J(j)],[I(j),J(j)]) = cos(lam(j)*k*delt);
            dum(I(j),J(j)) = -sin(lam(j)*k*delt);
            dum(J(j),I(j)) = sin(lam(j)*k*delt);
            Q = Q*dum;
        end
        % Rotate basis vectors.
        A = Q*E;
        % Project onto the new basis vectors.
        z = x*A;
        set(gtud.ph,'xdata',z(:,1),'ydata',z(:,2))
        set(gtud.H,'string',['Iteration: ',int2str(k)])
        drawnow
    else
        % give the user a chance to bring up the tourgui and save data
        % of they just might want to look at it.
        ud.gt = A;
        set(0,'userdata',ud)
        break
    end
    k = k + 1;
    gtud.k = k;
    set(Hf,'userdata',gtud)
end

function runkdimtour(x,maxit,k,typ,Hf,delt)

% KDIMTOUR    Grand Tour in k Dimensions - Torus Winding Algorithm
%
% special version for GUI
%   'a' produces Andrews' curves
%   'p' produces parallel coordinate plots (default)

gtud = get(Hf,'userdata');  % here we store the iteration and the step
[n,p] = size(x);
% Set up vector of frequencies.
N = 2*p - 3;
lam = mod(exp(1:N),1);
% % This is a small irrational number:
% delt = exp(1)^(-3); 
% Get the indices to build the rotations.
J = 2:p;
I = ones(1,length(J));
I = [I, 2*ones(1,length(J)-1)];
J = [J, 3:p];
E = eye(p,k);   % Basis vectors

if isempty(gtud)
    % Get an initial plot.
%     z = zeros(n,2);
%     gtud.ph = plot(z(:,1),z(:,2),'o','erasemode','normal');
    set(Hf,'backingstore','off','renderer','painters','DoubleBuffer','on')

    gtud.maxit = maxit;
    gtud.Hf = Hf;
    gtud.delt = delt;
    gtud.k = k;     % this is the number of display dimensions
    gtud.K = 1;     % This is the current iteration index
    gtud.maxit = maxit;
    if strcmp(typ,'p')
        gtud.typ = 'p';
        gtud.gtdisp = 3;    % since this is a parallel coordinates tour
        % get the axes lines
        kk = k;
        ypos = linspace(0,1,kk);
        xpos = [0 1];
        for i=1:kk            
            line(xpos,[ypos(i) ypos(i)],'color','k')
            text(-0.05,ypos(i), ['x' num2str(kk)] )
            kk=kk-1;
        end
        axis off
        gtud.hax = gca;
    else
        gtud.typ = 'a';
        gtud.gtdisp = 2;    % since this is a Andrews curves tour
        gtud.hax = axes('position',[0.05 0.075 0.9  0.8]);
        %     set(gtud.hax,'visible','off')
        set(gtud.hax,'xlimmode','manual')
        set(gtud.hax,'xlim',[-pi pi])
        axis off
    end
    gtud.H = uicontrol(gcf,'style','text',...
        'units','normalized',...
        'position',[0.01 0.01 0.2 0.05],...
        'horizontalalignment','left',...
        'string','Iteration: ');
    Hbuttstop = uicontrol(Hf,'style','pushbutton',...
        'units','normalized',...
        'position',[0.9 0.01, 0.1 0.05],...
        'string','Stop Tour',...
        'createfcn','ud=get(0,''userdata'');ud.gtstop=0;set(0,''userdata'',ud)',...
        'deletefcn','ud=get(0,''userdata'');ud.gtstop=1;set(0,''userdata'',ud)',...
        'callback','ud=get(0,''userdata'');ud.gtstop=1;set(0,''userdata'',ud)');
    Hbuttstart = uicontrol(Hf,'style','pushbutton',...
        'units','normalized',...
        'position',[0.65 0.01, 0.15 0.05],...
        'string','Resume Tour',...
        'callback',['ud=get(0,''userdata'');',...
            'ud.gtstop=0;set(0,''userdata'',ud);',...
            'tourgui(''grandtour'')']);
    % Set up the line handles - need n of these
    gtud.Hline = zeros(1,n);
    for i = 1:n
        gtud.Hline(i) = line('xdata',nan,'ydata',nan,'linestyle','-');
    end
    set(Hf,'userdata',gtud)
    drawnow
end
pause(0.01)
% Start the tour.
if strcmp(gtud.typ,'p')
    K = gtud.K;
    while K <= maxit
        ud = get(0,'userdata');
        if ud.gtstop == 0
            % tour is not stopped
            % Find the rotation matrix.
            Q = eye(p);
            for j = 1:N
                dum = eye(p);
                dum([I(j),J(j)],[I(j),J(j)]) = cos(lam(j)*K*delt);
                dum(I(j),J(j)) = -sin(lam(j)*K*delt);
                dum(J(j),I(j)) = sin(lam(j)*K*delt);
                Q = Q*dum;
            end
            % Rotate basis vectors.
            A = Q*E;
            % Project onto the new basis vectors.
            z = x*A;    
            parallelkd(gtud.Hline,z);
            set(gtud.H,'string',['Iteration: ',int2str(K)])
            drawnow
        else
            % give the user a chance to bring up the tourgui and save data
            % or look at it
            ud.gt = A;
            set(0,'userdata',ud)
            break
        end
        K = K + 1;
        gtud.K = K;
        set(Hf,'userdata',gtud)
    end   % while loop
else
    kk = k;    % number of desired dimensions
    theta = -pi:0.1:pi;    %this defines the domain that will be plotted
    y = zeros(n,kk);       %there will n curves plotted, one for each obs
    ang = zeros(length(theta),kk);   %each row must be dotted w/ observation
    % Get the string to evaluate function.
    fstr = ['[1/sqrt(2) '];   %Initialize the string.
    for i = 2:kk
        if rem(i,2) == 0
            fstr = [fstr,' sin(',int2str(i/2), '*i) '];
        else
            fstr = [fstr,' cos(',int2str((i-1)/2),'*i) '];
        end
    end
    fstr = [fstr,' ]'];
    k=0;
    % evaluate sin and cos functions at each angle theta
    for i=theta
        k=k+1;
        ang(k,:)=eval(fstr);
    end
    K = gtud.K;
    while K <= maxit
        ud = get(0,'userdata');
        if ud.gtstop == 0
            % Find the rotation matrix.
            Q = eye(p);
            for j = 1:N
                dum = eye(p);
                dum([I(j),J(j)],[I(j),J(j)]) = cos(lam(j)*K*delt);
                dum(I(j),J(j)) = -sin(lam(j)*K*delt);
                dum(J(j),I(j)) = sin(lam(j)*K*delt);
                Q = Q*dum;
            end
            % Rotate basis vectors.
            A = Q*E;
            % Project onto the new basis vectors.
            z = x*A;    
            axis off
            andrewskd(gtud.Hline,z,theta,ang);
            set(gtud.H,'string',['Iteration: ',int2str(K)])
            drawnow
        else
            % give the user a chance to bring up the tourgui and save data
            % or look at it
            ud.gt = A;
            set(0,'userdata',ud)
            break
        end   % if-else loop
        K = K + 1;
        gtud.K = K;
        set(Hf,'userdata',gtud)
    end % while loop
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       ANDREWS CURVES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function andrewskd(Hline,data,theta,ang)
[n,p] = size(data);
% keyboard
% Now generate a y for each observation
%
for i=1:n     %loop over each observation
  for j=1:length(theta)
    y(i,j)=data(i,:)*ang(j,:)'; 
  end
end

for i=1:n
  set(Hline(i),'xdata',theta,'ydata',y(i,:));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function parallelkd(Hline,x)

% If parallel coordinates, then change range.
% map all values onto the interval 0 to 1
% lines will extend over the range 0 to 1
md = min(x(:)); % find the min of all the data
rng = range(x(:));  %find the range of the data
xn = (x-md)/rng;
[n,p] = size(x);
ypos = linspace(0,1,p);
for i=1:n
    %     line(x(i,:),fliplr(ypos),'color','k')
    set(Hline(i),'xdata',xn(i,:),'ydata',fliplr(ypos))
end    


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -