我需要加速这段代码:它创建了一个多线性函数。我考虑了下面的方法,但不知道哪种方法有最大的好处。
我需要反复创建这样的初始化,并且我正在尽可能快地得到它。可能是因为我的代码速度慢的原因在其他地方,而不是这段代码,如果你这么认为的话!可能有一些优雅的函数式方法可以快速、简洁地编写这篇文章。我已经知道,由于mlfnneg的存在,整个代码可能很慢,但是为了加快速度,我需要确保该代码中的bug不是早期的,也就是在测试用例初始化期间。

%Creating a mlf to check the negativity of the multilinear function
% Example: 0<x_i<i/Density
lbs_str=containers.Map; % I need variables here only to initialise mlf at the end
ubs_str=containers.Map;
terms=containers.Map;
for i = 1:100
i=num2str(i); % possible to make this more elegant? contairers.Map requires string
lbs_str(i)=0;
ubs_str(i)=i/100;
terms(i)=struct('coeff',1,'vars',{{i}});
% Tested catstruct [1] to append new terms to the mlf_terms but not intended:
% it does not do {terms('1'), terms('2'),...} aka {terms.values}
end
lbs=mlfpoint(lbs_str);
ubs=mlfpoint(ubs_str);
mlf_terms=struct('const',0,'terms',{terms.values});
mlf=mlfcreate(mlf_terms);
mlfnneg(lbs,ubs,mlf)发布于 2013-08-26 12:43:21
您应该能够防止转换到字符串100次。
首先创建单元格数组中的所有字符串,然后循环遍历字符串。
虽然它没有出现在分析器上,但这看起来非常可疑:
i=num2str(i)
ubs_str(i)=i/100;现在,您将获得'i'的字符代码,并将其除以100。
https://codereview.stackexchange.com/questions/26385
复制相似问题