在赋值A(I)= B中,B ......中的元素数量?

时间:2013-09-20 11:33:05

标签: matlab

当我运行我的程序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

1 个答案:

答案 0 :(得分:2)

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

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

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