简体   繁体   中英

Subscripted assignment dimension mismatch

here is the code listing and i got the above mentiond error at line nests(r,c)=nests(r,c)+stepsize.*randn(size(nests(r,c))); please let me now what is wrong with my code as im new to matlab

    for r = 1:numb_of_nest % for each particle

      for c = 1:4
         u=randn(size(nests(r,c)))*sigma;
         v=randn(size(nests(r,c)));
         step=u./abs(v).^(1/beta);
         nests(r,c)=nests(r,c)+stepsize.*randn(size(nests(r,c)));

        % Apply simple bounds/limits
        ns_tmp=nests(r,c);
        I=ns_tmp<Lb(c);
        ns_tmp(I)=Lb(I);

        % Apply the upper bounds 
        J=ns_tmp>Ub(c);
        ns_tmp(J)=Ub(J);
        % Update this new move 
        nests(r,c)=ns_tmp;

         end
     end
     step=u./abs(v).^(1/beta);
     nests(r,c)=nests(r,c)+stepsize.*randn(size(nests(r,c)));

Here you're assigning a value to the variable step , but then using a different variable called stepsize that hasn't been assigned a value anywhere in this code. Is this intentional? If not, stepsize is probably some leftover variable from previous code which is messing up the dimensions and giving you this error.

In addition to the above, is nests an ordinary two-dimensional matrix in your code? If so, taking size(nests(r,c)) every time is unnecessary - since you're giving two subscripts, the result is only going to be 1 all the time. Or is nests a cell array perhaps? In that case, you might want to index using curly braces { } instead of ordinary parantheses, to get the size of the matrix that's sitting inside the cell.

This error happens when you assign a value of some dimension mxn to a subscripted variable of different dimension.

In your case, assuming nests has no third dimension, you're assigning to a scalar ( 1x1 ) variable. This only works if the value you're trying to assign also is a scalar. Since you get the error, it probably isn't. The only place where your dimensions can be non-scalar is stepsize , so to fix this error, make sure stepsize is a scalar value .

According to the definition you gave in an earlier comment ( stepsize=0.01*step.*(nests(r,c)-best); ), this problem translates to make sure best is a scalar value . Possibly by subscripting, I can't tell you exactly how since I don't know what best is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM