-3

当我运行我的程序进行 4 次迭代时没有问题,但是如果我运行它超过 4 次,我会收到以下错误

In an assignment  A(I) = B, the number of elements in B
and
I must be the same.

对于以下行

Corresponding_value_of_x1(i)=x1(f==Lowest_value_of_the_objective_function(i));

请帮忙。

maxit=5
iga=1

x1=(6.*bin2dec(String_1))./1023
x2=(6.*bin2dec(String_2))./1023


for i=1:1:maxit


f=(x1.^2+x2-11).^2+(x1+x2.^2-7).^2;



  %Displaying results from the iteration
         i;
         Lowest_value_of_the_objective_function(i)= min(f);                                                                         

        Corresponding_value_of_x1(i)=x1(f==Lowest_value_of_the_objective_function(i)); 


        nx1=(6.*bin2dec(New_string(1)))./1023;
        nx2=(6.*bin2dec(New_string(2)))./1023;

        x1=nx1;
        x2=nx2;


end


         Corresponding_value_of_x1
4

1 回答 1

2

看起来您正在尝试找到向量的最小值,然后是该值的相应位置。 min将立即完成所有这些,避免您遇到的问题:

[Lowest_value_of_the_objective_function(i) Corresponding_value_of_x1(i)] = min(f);

请注意,您的错误正在发生,因为相同的最小值出现了不止一次。此代码将返回这些最小值中的第一个。如果您想要不同的行为,则必须对其进行编码。

于 2013-09-20T12:56:12.597 回答