跳转到内容

User:Tttfffkkk/Madlag程序

维基百科,自由的百科全书

算法[编辑]

穷举
  • clc;clear;
    numofvalues=3;%变量个数
    region=[1:50];%取值
    condition='x1+x2+x3==10';%条件
    showoutput=0;output='x1+x2';%额外输出
    
    way=0;
    scrsz=get(0,'ScreenSize');scx=scrsz(3);scy=scrsz(4);
    fig1=figure('position',[scx/2 scy/3 500 400]);
    way1=uicontrol('style','pushbutton','string','一般穷举',...
        'fontsize',30,'position',[100 310 300 80],...
        'callback','way=1;close all;','background','white');
    way2=uicontrol('style','pushbutton','string','顺序穷举',...
        'fontsize',30,'position',[100 210 300 80],...
        'callback','way=2;close all;','background','white');
    way3=uicontrol('style','pushbutton','string','互异顺序穷举',...
        'fontsize',30,'position',[100 110 300 80],...
        'callback','way=3;close all;','background','white');
    way4=uicontrol('style','pushbutton','string','互异穷举',...
        'fontsize',30,'position',[100 10 300 80],...
        'callback','way=4;close all;','background','white');
    while(way==0)
        pause(0.005);
    end
    
    fig2=figure('position',[scx/2 scy/3 500 400]);
    stop=uicontrol('style','toggle','string','暂停',...
        'fontsize',30,'position',[0 0 500 400],'background','white');
    Lregion=length(region);
    command='';t=0;
    switch(way)
        case 1
            for i=1:numofvalues
                command=[command 'for t' num2str(i) '=1:Lregion,'];
            end
        case 2
            command=[command 'for t1=1:Lregion,'];
            for i=2:numofvalues
                command=[command 'for t' num2str(i) '=t'...
                    num2str(i-1) ':Lregion,'];
            end
        case 3
            command=[command 'for t1=1:Lregion,'];
            for i=2:numofvalues
                command=[command 'for t' num2str(i) '=t'...
                    num2str(i-1) '+1:Lregion,'];
            end
        case 4
            for i=1:numofvalues
                command=[command 'for t' num2str(i) '=1:Lregion,'];
            end
            for i=1:numofvalues
                for j=i+1:numofvalues
                    condition=[condition ' && t' num2str(i)...
                        '~=t' num2str(j)];
                end
            end
        otherwise
            error('No way is choosed');
    end
    
    for i=1:numofvalues
        command=[command 'x' num2str(i) '=region(t' num2str(i) ');'];
    end
    command=[command,'if(eval(condition)),'];
    for i=1:numofvalues
        command=[command,'fprintf(''%d '',x' num2str(i) ');'];
    end
    if(showoutput)
        command=[command,'fprintf(''output=%g'',eval(output));'];
    end
    command=[command,'fprintf(''\n'');'];
    command=[command,'end,'];
    command=[command,'if(t==1e3)'];
    command=[command,'t=0;pause(0.005);'];
    command=[command,'if get(stop,''value'')==1,'];
    command=[command,'close all;return;end,'];
    command=[command,'end,t=t+1;'];
    for i=1:numofvalues
        command=[command,'end,'];
    end
    command=command(1:end-1);
    eval(command);
    close all;
    
默書系統
  • 使用前請先把字詞數據保存到book.mat中,第一列放中文,第二列放英文,如: 桌子 table 椅子 chair
    clc;clear;
    load book.mat;
    n=size(book,1);
    correct=0;
    for i=randperm(n)
        fprintf('%s\n',book{i,1});
        x=input('英文:','s');
        wrong=0;
        if(length(x)==length(book{i,2}))
            if(x==book{i,2})
                fprintf('正确\n');
                correct=correct+1;
            else
                wrong=1;
            end
        else
            wrong=1;
        end
        if(wrong)
            fprintf('错误\n');
            fprintf('答案是%s\n',book{i,2});
        end
        x=input('是否继续?\n1.继续 2.结束\n');
        clc;
        if(x==2)break;end
    end
    score=fix(100*correct/n);
    fprintf('你的分数是:%d\n',score);
    

函数[编辑]

欧拉函数
  • function varphi=varphi(n)
    %VARPHI the Euler function of an integer
    %  Use it with VARPHI(n)
    %  For example,VARPHI(6) returns 2
    
    if(n-fix(n)~=0 || n<0)
        error('k must be a positive integer');
    end
    f=factor(n);p=unique(f);varphi=1;
    if(n>1)
        for i=1:length(p)
            r(i)=length(find(f==p(i)));
            varphi=varphi*p(i)^(r(i)-1)*(p(i)-1);
        end
    end
    end
    
幂函数模运算
  • function y=pmod(n,m,k)
    %PMOD calculates the expression n^m(mod k)
    %  Use it with PMOD(n,m,k)
    %  For example,PMOD(2,2,3) returns 1
    
    if(any(([n,m,k]-fix([n,m,k])~=0)))
        error('n,m,k must be integer');
    end
    v=varphi(k);
    maxr=length(factor(k))-length(unique(factor(k)))+1;
    m=mod(m-maxr,v)+maxr;
    %if(gcd(m,v)==1)m=mod(m-maxr,v)+maxr;end
    if(k<0)k=abs(k);end
    if(m==0)
        y=1;
    elseif(m==1)
        y=mod(n,k);
    else
        y=mod(n,k);
        for i=1:m-1
            y=mod(y*n,k);
        end
    end
    end
    
一般函数模运算
  • function value=smod(str,k)
    %SMOD calculates the module k of the expression
    %  Use it with SMOD(str,k)
    %  For example,SMOD('10^(10^10)',7) returns 4
    
    if(k-fix(k)~=0)
        error('k must be integer');
    end
    if(k<0)k=abs(k);end
    exception=0;
    str2=str-0;
    f=[];chain=[];chain1=0;chain2=0;
    for i=1:length(str2)
        if(chain1==1)
            chain2=1;
            if(any(str2(i)==['0'-0:'9'-0]))
                f=[f i];
            else
                chain1=0;
                chain=[chain i-1];
            end
        else
            if(any(str2(i)==['0'-0:'9'-0 '-'-0]))
                f=[f i];chain=[chain i];chain1=1;
            end
        end
    end
    chain=[chain i];
    
    leftb=strfind(str,'(');
    rightb=strfind(str,')');
    if(length(leftb)~=length(rightb))
        error('blanklet error');
    end
    plb=strfind(str,'^(');
    groupb=[];i=0;
    while(~isempty(leftb))
        i=i+1;
        rbend=find(rightb>leftb(end),1);
        groupb=[groupb;leftb(end),rightb(rbend)];
        leftb=setdiff(leftb,leftb(end));
        rightb=setdiff(rightb,rightb(rbend));
    end
    leftb=strfind(str,'(');
    rightb=strfind(str,')');
    
    if(~isempty(plb))
        strplb1=str([1:plb(1)]);
        numb=smod(str([plb(1)+2:groupb(groupb(:,1)==plb(1)+1,2)-1]),varphi(k));
        strplb2=num2str(numb);
        if(groupb(groupb(:,1)==plb(1)+1,2)==length(str))
            strplb3=[];
        else
            strplb3=str([groupb(groupb(:,1)==plb(1)+1,2)+1:end]);
        end
        strplb=[strplb1 strplb2 strplb3];
        value=smod(strplb,k);
    else
        if(~isempty(leftb))
            if(leftb(1)==1)
                strb1=[];
            else
                strb1=str([1:leftb(1)-1]);
            end
            numb=smod(str([leftb(1)+1:groupb(groupb(:,1)==leftb(1),2)-1]),k);
            strb2=num2str(numb);
            if(rightb(1)==length(str))
                strb3=[];
            else
                strb3=str([groupb(groupb(:,1)==leftb(1),2)+1:end]);
            end
            str3=[strb1 strb2 strb3];
            value=smod(str3,k);
        else
            power=strfind(str,'^');
            for i=1:length(power)
                s=0;
                lc=find(chain==power(i)-1,1,'last');
                rc=find(chain==power(i)+1,1);
                s=s+~isempty(lc)+~isempty(rc);
                if(s==2)
                    exception=1;
                    strn=str([chain(lc-1):chain(lc)]);
                    strm=str([chain(rc):chain(rc+1)]);
                    n=str2num(strn);m=str2num(strm);
                    if(chain(lc-1)==1)
                        strpmod1=[];
                    else
                        strpmod1=str([1:chain(lc-1)-1]);
                    end
                    strpmod2=num2str(pmod(n,m,k));
                    if(chain(rc+1)==length(str))
                        strpmod3=[];
                    else
                        strpmod3=str([chain(rc+1)+1:end]);
                    end
                    str3=[strpmod1 strpmod2 strpmod3];
                    value=smod(str3,k);
                    break;
                end
            end
            if(exception==0)
                value=mod(eval(str),k);
            end
        end
    end
    end
    
从map找颜色
  • function value=findcolor(color,map,delta)
    %FINDCOLOR finds the responding color from the given map
    %  Use it with FINDCOLOR(color,map,delta)
    %  FINDCOLOR returns the value of the responding color among [0,255]
    %  delta is the biggest error of color defaulted 0
    
    if(nargin<2)
        error('Not enough input arguments');
    elseif(nargin==2)
        delta=0;
    end
    
    value=[];
    for i=1:size(map,1)
        d(i)=sum(abs(map(i,:)-color));
    end
    value=[value find(d<=delta)-1];
    if(isempty(value))
        mind=min(d);
        value=find(d==mind)-1;
    end
    end
    
绘图或合成图
  • function [image]=imdraw(varargin)
    %IMDRAW draw the color on the image by restrictions
    %  For 2-D image matrix
    %    Use it with IMDRAW(image,map,color,<restrictions>)
    %  For 3-D image matrix
    %    Use it with IMDRAW(image,color,<restrictions>)
    %  color can be [R G B] or another image
    %  <restrictions> including:
    %    'tcolor':IMDRAW(image,color,'tcolor',<tcolor>)
    %      The target color that you want to change.
    %      Write <tcolor> in the form [R G B].
    %    'colorerror':IMDRAW(image,color,'colorerror',<colorerror>)
    %      Accepted difference from the target color.
    %    'rect':IMDRAW(image,color,'rect',<rect>)
    %      Draw the color inside the rectangle.
    %      Write <rect> in the form [x1 y1 x2 y2].
    %      (x1,y1),(x2,y2) are the opposite points of the rectangle.
    %    'fun':IMDRAW(image,color,'fun',<fun>)
    %      Draw the color when the function values true.
    %      Write <fun> in the form 'f(x,y)' such as 'x^2+y^2<=1'
    image=varargin{1};
    if(ismatrix(image))
        if(length(varargin)<3)
            error('Not enough input arguments');
        end
        map=varargin{2};color=varargin{3};
        varargin=varargin(4:end);
    elseif(ndims(image)==3)
        if(length(varargin)<2)
            error('Not enough input arguments');
        end
        color=varargin{2};
        varargin=varargin(3:end);
    end
    
    tcolor=[];rect=[];fun=[];colorerror=0;
    while(~isempty(varargin))
        if(strcmp(varargin{1},'tcolor'))tcolor=varargin{2};end
        if(strcmp(varargin{1},'colorerror'))colorerror=varargin{2};end
        if(strcmp(varargin{1},'rect'))rect=varargin{2};end
        if(strcmp(varargin{1},'fun'))fun=varargin{2};end
        varargin=varargin(3:end);
    end
    
    if(ismatrix(image))
        image=imdraw1(image,map,color,colorerror,tcolor,rect,fun);
    elseif(ndims(image)==3)
        image=imdraw2(image,color,colorerror,tcolor,rect,fun);
    end
    end
    
    function [image]=imdraw1(image,map,color,colorerror,tcolor,rect,fun)
    [n1,n2]=size(image);
    if(strcmp(class(color),class([0 0 0])))
        value2=findcolor(color,map);
        row=ceil(length(value2)*rand);
    end
    if(~isempty(tcolor))
        value1=findcolor(tcolor,map,colorerror);
    end
    
    for y=1:n1
        for x=1:n2
            r=1;
            if(~isempty(tcolor))
                r=r*any(image(y,x)==value1);
            end
            if(~isempty(rect))
                x1=min(rect(1),rect(3));
                x2=max(rect(1),rect(3));
                y1=min(rect(2),rect(4));
                y2=max(rect(2),rect(4));
                r=r*(x1<=x)*(x<=x2)*(y1<=y)*(y<=y2);
            end
            if(~isempty(fun))
                r=r*eval(fun);
            end
            if(r==1)
                if(strcmp(class(color),class([0 0 0])))
                    image(y,x)=value2(row);
                else
                    image(y,x)=color(y,x);
                end
            end
        end
    end
    end
    
    function [image]=imdraw2(image,color,colorerror,tcolor,rect,fun)
    [n1,n2,n3]=size(image);
    for y=1:n1
        for x=1:n2
            r=1;
            if(~isempty(rect))
                x1=min(rect(1),rect(3));
                x2=max(rect(1),rect(3));
                y1=min(rect(2),rect(4));
                y2=max(rect(2),rect(4));
                r=r*(x1<=x)*(x<=x2)*(y1<=y)*(y<=y2);
            end
            if(~isempty(fun))
                r=r*eval(fun);
            end
            d=0;
            for k=1:n3
                if(~isempty(tcolor))
                    z=image(y,x,k);
                    if(z<tcolor(k))d=d+tcolor(k)-z;
                    else d=d+z-tcolor(k);end
                end
            end
            if(d<=colorerror && r==1)
                if(strcmp(class(color),class([0 0 0])))
                    image(y,x,:)=color;
                else
                    image(y,x,:)=color(y,x,:);
                end
            end
        end
    end
    end
    
传送到目标文件夹
  • function tp(x)
    %TP teleports the current working directory to your desire place
    %  Use it with tp(x)
    %  x can be number or string as defined in route.txt
    clc;
    fid=fopen('route.txt');
    C=textscan(fid,'%d%s%s');
    fclose(fid);
    selection=[C{2},C{3}];
    if(ismember(x,C{1}))
        cd(selection{find(C{1}==x,1),2});
    elseif(ischar(x))
        for i=1:size(selection,1)
            if(strcmp(x,selection{i,1}))
                cd(selection{i,2});
            end
        end
    end
    
    end
    

    请在route.txt中定义目标路径并放到与tp.m同一文件夹上,例如:

    0	fun	D:\我的madlag函数
    1	N	E:\笔记\matlab\madlag中级课程
    2	data	E:\笔记\数据分析