Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:如何将变量的累进值与其进行比较';s以前的最佳状态并重新分配给它';那是以前最好的吗?_Matlab_Loops_If Statement_For Loop - Fatal编程技术网

Matlab:如何将变量的累进值与其进行比较';s以前的最佳状态并重新分配给它';那是以前最好的吗?

Matlab:如何将变量的累进值与其进行比较';s以前的最佳状态并重新分配给它';那是以前最好的吗?,matlab,loops,if-statement,for-loop,Matlab,Loops,If Statement,For Loop,我有一段代码来评估循环中某个变量的性能,这样我们就可以不断检查它的进度,并将其与有史以来的最佳值进行比较,如果新分配的值被评估为不优于该变量在其最佳值下的性能,则将其分配回该最佳值 for (True) err = sum(Y*X'*w <=0); %measure the performance of w for fixed X and Y variables. err_best = sum(Y*X'*wbest <=0); %measure the performanc

我有一段代码来评估循环中某个变量的性能,这样我们就可以不断检查它的进度,并将其与有史以来的最佳值进行比较,如果新分配的值被评估为不优于该变量在其最佳值下的性能,则将其分配回该最佳值

for (True) 

err = sum(Y*X'*w <=0);   %measure the performance of w for fixed X and Y variables.
err_best = sum(Y*X'*wbest <=0);  %measure the performance of w best
if(err <= err_best)              %if performance of newly assigned value of w is better than that of its career best wbest then update wbest with this newly identified best value.
    wbest = w;            %update the value of wbest if the w is evaluated to be better than the previous best wbest.
end
w = wbest;                 % wbest is updated to the best value evaluated thus far and assign to w this value.

% Code to change w value.

end
for(真)

err=sum(Y*X'*w您始终可以在
wbest
中附加最佳值,并将
wbest(end)
用作“至今最佳值”,如:

for (True) 

err = sum(Y*X'*w <=0);   %measure the performance of w for fixed X and Y variables.
err_best = sum(Y*X'*wbest(end) <=0);  %measure the performance of w best
if(err <= err_best)              %if performance of newly assigned value of w is better than that of its career best wbest then update wbest with this newly identified best value.
    wbest = [wbest w];            %update the value of wbest if the w is evaluated to be better than the previous best wbest.
end
w = wbest(end);                 % wbest is updated to the best value evaluated thus far and assign to w this value.

% Code to change w value.

end
for(真)

err=sum(Y*X'*w)这种方法也不能解决问题。不过,谢谢。如果你看到问题所附的代码中有任何错误,你愿意指出吗?谢谢你详细说明“不能解决问题”在对试图免费帮助你的人投否决票之前?
w
的形式是什么?我从来没有投过否决票。我用你的方法得到的结果与我用帖子中附带的代码得到的结果相同。我更想知道我的方法在代码中是否有任何错误。谢谢。假设你已经初始化了
w
=And
wbest
=B在循环之外。在第一次迭代结束时,您将始终拥有
w=wbest(=A或B)
。因此在第二次迭代中,您将始终拥有
err==err\u best
,然后您将拥有
wbest=w(=A或B)
,然后是
w=wbest(=A或B)
。因此,您的迭代只会继续循环,但什么也不做。此外,您可能需要重新考虑您的算法。“将其分配回此最佳值”这意味着您只需返回到上一个计算步骤。除非您的算法第二次以不同的方式执行该步骤,否则这不会有任何帮助。如果您想要一个真正快速的解决方案,我们需要一个具有所需输出的n示例数据集。@MingjingZhang有更改w值的代码,该代码未显示,因此在第二次迭代中,它将具有dif不同的错误。你为什么要在最后做w=wbest?新的w依赖于旧的w吗?如果它依赖于旧的w,而下一个w给出的错误比最好的更糟,你的算法将被卡住。我解决了它。感谢所有指出这段代码冗余的人。我删除了最后一个命令,并以不同的方式使用wbest,所以我得到了结果如预期。我感谢所有人。命令w=wbest;花费相当大。特别感谢@MingjingZhang