声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 10082|回复: 5

[编程技巧] Subscripted assignment dimension mismatch 出错请教

[复制链接]
发表于 2007-8-3 16:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
我是初学者,在借助代码修改程序中出现问题,苦思多日不见出路,特向高手请教.程序如下:
global x;
x1(1,:)=[0.671796 0.919430 0.941397 0.438988 0.866638 0.201446 0.523668 0.408518 0.373441 0.491071 1.114654 1.155400 0.961947 1.094813 1.123866 0.350057 0.391865 0.246599 0.538903 0.502409 0.406430 0.456700 0.460840        0.397420 0.436510 0.483510 0.374620        0.377710 0.590160 0.523430 0.669290        0.665860 0.749950 0.605870 0.710390 0.625950 0.711690 0.499100 0.638230 0.511580 0.663890 0.636220 0.758930        0.528160 3.038550 0.798380 0.786560        0.771090 0.913760 0.606690];
x1(2,:)=[0.767520 0.676370 0.597390 0.562620 0.701980 0.727960 0.835730 0.571260 0.854420 0.842830 0.597410 0.649750 0.509080 0.708950 0.795860 0.955310 0.871440 0.914960 0.815090 0.906860 0.491800 0.541700 0.545900        0.470700 0.499200 0.552200 0.486200        0.458700 0.619100 0.575000 0.658900        0.660500 0.694800 0.641200 0.678700        0.622300 0.672600 0.540700 0.650700        0.541100 0.665800 0.660700 0.766800        0.580900 0.662400 0.757700 0.775700        0.742300 0.819000 0.628400];
x1(3,:)=[0.425500 1.400000 0.259300 0.255860 0.071340 0.041920 0.196130 0.119700 0.188000 0.194500 0.583500 0.230900 1.080000 0.068720 0.176560 0.043970 0.140240 0.160500 0.250200 0.174000 0.008670 0.713800 0.625100        0.172800 0.026740 0.318590 0.024710        0.170000 0.657100 0.303000 0.762700        1.320000 0.346400 0.276750 0.194680        0.281690 0.116280 0.159600 0.112300        0.119600 0.486700 1.150000 0.368300        0.351650 0.080900 0.208280 0.182530        0.105700 0.079850 0.002050];
success=0;
PopSize=20;
MaxIt=40;
iter=1;
fevals=0;%function evaluation'counter
maxw=1.2;%maximum interia weight's value
minw=0.1;%minimum interia weight's value
c1=0.5;
c2=0.5;
inertdec=(maxw-minw)/MaxIt;% weight decrease by same value, search change function of weight
w=maxw;
f='gold';
dim=3;
ErrGoal=1e-4;
%initializing swarm and velocity
vel=rand(dim,PopSize);

for i=1:3
   popul(i,:)=rand(1,PopSize)*(max(x1(i,:))-min(x1(i,:)))+min(x1(i,:))
end
vel=rand(dim,PopSize);
%evaluation initial population
for i=1:PopSize
     x1=popul(1,i); %popul is 2D array,2D particle population
     x2=popul(2,i);  
     x3=popul(3,i);
   
  
     fpopul(i)=feval(f,x1,x2,x3);   %fpopul is 1D array,function values of 2D particle population
     fevals=fevals+1;
     ppopul(:,i,iter)=popul(:,i);
end
bestpos=popul; % positions of  initial particle
fbestpos=fpopul;% func values of initial particle
%ppopul(:,:,iter)=ppopul;
% finding best particle in initial
[fbestpart(iter),g]=min(fpopul);% min value of population and index.
lastbpf=fbestpart(iter);% current values of optimal particle

%swarm evolution loop
while(iter<MaxIt)
    iter=iter+1;
    % update the value of inertia weight w
    if(iter<=MaxIt)
        w=maxw-(iter-1)*inertdec;
    end
   
    %velocity update
    for i=1:PopSize
        A(:,i)=bestpos(:,g);% optical position of each particle is g.
    end   
        R1=rand(dim,PopSize);
        R2=rand(dim,PopSize);
        vel=w*vel+c1*R1.*(bestpos-popul)+c2*R2.*(A-popul);%local optical is bestpos,global optical is A.
        %swarm update
        popul=popul+vel;
        for i=1:3
            for j=1:PopSize
                 ppopul(i,j,iter)=popul(i,j);
             end
         end
            for i=1:PopSize
            x1=popul(1,i);
            x2=popul(2,i);
            x3=popul(3,i);
      

            fpopul(iter,i)=feval(f,x1,x2,x3);
            fevals=fevals+1;
        end
        
        
        for i=1:PopSize
          if(fpopul(iter,i)<fbestpos(i))
           fbestpos(i)=fpopul(iter,i);
            bestpos(:,i)=popul(:,i);
        end
        end
              
          [fbestpart(iter),g]=min(fbestpos);
          if (abs(fbestpart(iter)-fbestpart(iter-1))<=ErrGoal)&(abs(fpopul(iter,1)-fpopul(iter-1,1))<=ErrGoal)
           break;
       end
    end
   
     xmin=popul(:,g)
    fxmin=fbestpos(g)
    bestpos
      subplot(2,2,1)
    a=iter/3;
   
    for i=1:a
       i=i*2;
       % text(xx1(i,1),yy1(i,1),zz1(i,1),['\leftarrow(',num2str(i),')'],'FontSize',6.0)
       plot3(ppopul(1,1,i),ppopul(2,1,i),ppopul(3,1,i),'linestyle','--','marker','.','markersize',3)
       xlabel('萼片长度/cm','fontsize',8),ylabel('花瓣高度/cm','fontsize',8),zlabel('花瓣宽度/cm','fontsize',8)
       title('粒子1优化过程轨迹','FontSize',8)
       axis([2.5  7 2 5 0 4])
            hold on

    end
   
    iter
    fevals

      

  function sum=gold(x1,x2,x3)
   global x;
  sum=0;
     x1(1,:)=[0.671796 0.919430 0.941397 0.438988 0.866638 0.201446 0.523668 0.408518 0.373441 0.491071 1.114654 1.155400 0.961947 1.094813 1.123866 0.350057 0.391865 0.246599 0.538903 0.502409];
     x1(2,:)=[0.767520 0.676370 0.597390 0.562620 0.701980 0.727960 0.835730 0.571260 0.854420 0.842830 0.597410 0.649750 0.509080 0.708950 0.795860 0.955310 0.871440 0.914960 0.815090 0.906860];
     x1(3,:)=[0.425500 1.400000 0.259300 0.255860 0.071340 0.041920 0.196130 0.119700 0.188000 0.194500 0.583500 0.230900 1.080000 0.068720 0.176560 0.043970 0.140240 0.160500 0.250200 0.174000];
     
     for i=1:20
               
      sum=sum+0.126*(x(1,i)-x1)^2+0.505*(x(2,i)-x2)^2+0.368*(x(3,i)-x3)^2;
     end

运行结果是:
??? Subscripted assignment dimension mismatch.
可能是gold生成过程中出错,可是不清楚怎么错的,
回复
分享到:

使用道具 举报

发表于 2007-8-3 16:22 | 显示全部楼层
原帖由 pier 于 2007-8-3 16:06 发表
我是初学者,在借助代码修改程序中出现问题,苦思多日不见出路,特向高手请教.程序如下:
global x;
x1(1,:)=[0.671796 0.919430 0.941397 0.438988 0.866638 0.201446 0.523668 0.408518 0.373441 0.491071 1.11 ...


根据matlab的出错提示,定位到程序具体代码中,然后设置断点,调试一下。详情请参阅各个置顶贴
 楼主| 发表于 2007-8-3 16:47 | 显示全部楼层

回复 #2 eight 的帖子

非常高兴能得到这么快的回复!!由衷地感到这个论坛的的负责人都特别认真!!谢谢eight!
继续上面的问题:
运行结果是:Error in ==> pso>gold at 124
     x1(1,:)=[0.671796 0.919430 0.941397 0.438988 0.866638 0.201446 0.523668 0.408518 0.373441 0.491071 1.114654 1.155400 0.961947 1.094813 1.123866 0.350057 0.391865 0.246599 0.538903 0.502409];

Error in ==> pso at 34
     fpopul(i)=feval(f,x1,x2,x3);   %fpopul is 1D array,function values of 2D particle population
因为采用的是gold优化序列(这个就是我用原来的标题的原因),结果运行的维度错误确实应该是关于gold,因此我主要想就gold这个优化序列指令的使用注意事项请教高手?
 楼主| 发表于 2007-8-3 19:46 | 显示全部楼层

自己解答

我自己来解答吧
运行过程中没有处理好下面一段:
for i=1:PopSize
     x1=popul(1,i); %popul is 2D array,2D particle population
     x2=popul(2,i);  
     x3=popul(3,i);
   
  
     fpopul(i)=feval(f,x1,x2,x3);   %fpopul is 1D array,function values of 2D particle population
     fevals=fevals+1;
     ppopul(:,i,iter)=popul(:,i);
end
因此,fpopul(i)=feval(f,x1,x2,x3)中的x2,x3等的脚标出现问题

评分

1

查看全部评分

发表于 2010-6-25 10:56 | 显示全部楼层
请问楼主是怎么解决的这个问题呢?我现在也遇到相似的问题了,想不出解决办法
发表于 2010-6-25 21:19 | 显示全部楼层
原帖由 苏静207 于 2010-6-25 10:56 发表
请问楼主是怎么解决的这个问题呢?...

5F???
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 20:53 , Processed in 0.057330 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表