数学建模实录(3)

类别:软件工程 点击:0 评论:0 推荐:

1。函数名:CrtPower(a)

作用:计算矩阵中的向量权

优点:直接由原始数据可以换算出矩阵中的向量权

源代码:

function rstPower=CrtPower(a)
%**********************************************************
%  This is Help Information About Power() Function.
%           Find the min number in the matrix.
%         Verison:1.2.0  Finish Date:30/08/2004
%   Usage:
%       Power(a)      %a is matrix .
%       return a rstPower.
%***********************************************************

if nargout>1
    error('Too many output arguments!');
else
 if (nargin<1 | nargin>=2)
        error('Too many input arguments!');
    else
        SumCols=length(a(1,:));%Get the Cols of the matrix.
  if SumCols>=3
            SumRows=length(a(:,1));%Get the Rows of the matrix.
   rstPower=zeros(SumRows);
      for Col=2:SumCols-1
                ParCol=a(:,Col);
                BefCol=a(:,Col-1);
                AftCol=a(:,Col+1);
    for RowCount=1:SumRows;
                    for tmpRow=1:SumRows;
                        if RowCount==tmpRow
                            rstPower(RowCount,tmpRow)=0;
                        else
                            if ParCol(RowCount)-BefCol(tmpRow)>=0
                                tmpM=ParCol(RowCount)-BefCol(tmpRow);
                                if AftCol(RowCount)-ParCol(tmpRow)>=0;
                                    tmpN=AftCol(RowCount)-ParCol(tmpRow);
                                else
                                    tmpN=AftCol(RowCount)-ParCol(tmpRow);
                                end;
                                rstPower(RowCount,tmpRow)=rstPower(RowCount,tmpRow)+abs(tmpM)+abs(tmpN)-(Col-2)*abs(tmpM);
                            else
                                tmpM=(SumCols-Col+1)*(ParCol(RowCount)-BefCol(tmpRow));
                                if AftCol(RowCount)-ParCol(tmpRow)>=0;
                                    tmpN=AftCol(RowCount)-ParCol(tmpRow);
                                    rstPower(RowCount,tmpRow)=rstPower(RowCount,tmpRow)+abs(tmpM+tmpN)-(Col-2)*abs(tmpM);
                                else
                                    tmpN=AftCol(RowCount)-ParCol(tmpRow);
                                    rstPower(RowCount,tmpRow)=rstPower(RowCount,tmpRow)+abs(tmpM)+abs(tmpN)-(Col-2)*abs(tmpM);
                                end;
                            end;
                        end;%End if
                     end;%End tmpRow for
                end;%End RowCount for
   end;%End Col For
        else
            error('Length of the martrix is too short!The minium number is 3!');
        end; %End if SumCols>=3
    end;%End If (nargin<1 | nargin>=2)
end;

2。函数名:FindMinParam(a,iCount,Diff)

作用:查找矩阵中指定的iCount个最小数。查找同行中(Diff=0,默认值)、不同行(Diff=1)中最小值

优点:直接给出数据就可以查找出矩阵中较小元素所在的行和列

源代码:

function rst=FindMinParam(a,iCount,Diff)
%**********************************************************
%  This is Help Information About FindMinParam() Function.
%  Find the min number in defferent Rows and Cols the matrix.
%           Verison:1.1.2  Finish Date:28/08/2004    
%   Usage:
%       FindMinParam(a,iCount,Diff)
%   a is matrix .
%   iCount is Counter.
%   Diff is the parame to Find the mininum in Different Row.       
%***********************************************************

if nargout>1
    error('Too many output arguments!');
else
 if (nargin==0 | nargin>3)
        error('Too many input arguments!');
 else
        Cols=length(a(1,:));
  Rows=length(a(:,1));
        if nargin==1
            iCount=Rows * Cols;
            Diff=0;
        end;
        if nargin==2 | nargin==3
            if iCount>Rows * Cols
                error('The search number is too big!');
                iCount=Rows * Cols;
            elseif iCount<1
                error('The mininum is 1');
                iCount=1;
            else
                iCount=iCount;
            end;
            if nargin==3
                if Diff==1
                    Diff=1;
                else
                    Diff=0;
                end;
            else
                Diff=0;
            end;
        end;
       
        rst=zeros(iCount,3);
  for Count=1:iCount
            Succ=0;
            for RowCount=1:Rows;
                for ColCount=1:Cols;
           if (min(min(a))==a(RowCount,ColCount));   
                        tmpMin=min(min(a));                    
            tmpRow=RowCount;                       
            tmpCol=ColCount;
                        if Diff==1
                            a(RowCount,:)=inf;                    
                a(:,ColCount)=inf;
                        else
                            a(RowCount,ColCount)=inf;
                        end;
                        Succ=1;                                
                        break;                                 
                    end;
                end     %End For
                if Succ==1                                     
                    break;
                end;
            end;
            if Succ==1                                         
                rst(Count,1)=tmpMin;
                rst(Count,2)=tmpRow;
                rst(Count,3)=tmpCol;
            end;
  end;
  disp(rst);
 end;
end;

面对问题:

1.CrtPower函数还需要继续改进,测试其是否能推广到更多的问题; 2.能否将FindMinParam程序添加功能,使用户指定所要查找的数据。 3.能否将最终的结果用直观图表示出来?   总结:        本次模型在推广方面有了改进,大家也是尽心去将本次实验作的更好,值得以后发扬。程序方面也有了一定的提高。  

本文地址:http://com.8s8s.com/it/it36287.htm