【问题标题】:Erro:Edge vector must be monotonically non-decreasing错误:边向量必须是单调非递减的
【发布时间】:2014-03-18 22:37:32
【问题描述】:

我的问题在于 MC​​solutionupdated() 函数,因为我收到错误“边缘向量必须是单调非递减的”,因此无法运行 20,000 多次迭代。有时,我什至为 final_matrix 获取 Inf 元素(当我没有收到此错误时),这也没有任何意义。当我运行 Nielsennewupdated(casechoice, no_iterations) 时,我可以运行它进行尽可能多的模拟,并且我永远不会获得 Inf 元素。所以,我认为 MCsolutionupdated() 肯定有问题。

function [final_matrix, thresh_strain] = MCsolutionupdated()

no_iterations = input('No. of iterations?:');

thresh_strain = zeros(1,no_iterations*16);

casechoice =input('Enter 1 for 1st Layup and 2 for 2nd layup:');

 J = Nielsennewupdated(casechoice, no_iterations);
 thresh_strain = J;


roundedValues = round(thresh_strain/.0001)*0.0001;
myUniqueValues = unique(roundedValues);
i = numel(myUniqueValues);
nelements  = hist(thresh_strain(:),myUniqueValues); 

for i=1:i
    percent(i)  = (nelements(1,i)/numel(thresh_strain))*100;
end

final_matrix = [myUniqueValues' percent'];

% uniqueValues,~,uniqueIndex] = unique(ans);
% frequency = accumarray(uniqueIndex(:),1)./numel(ans);

header = {'Threshold Strain' 'Probability of occurrence'};
xlswrite('results.xlsx', header) 
xlswrite('results.xlsx', final_matrix,'A2')

谢谢

【问题讨论】:

  • 请用行号发布整个错误消息,以便我们查看它发生的位置。你做过任何调试吗?错误是在调用Nielsennewupdated 之前、在Nielsennewupdated 内部还是在Nielsennewupdated 返回值之后发生?如果Nielsennewupdated 返回该向量,也不需要预先分配thresh_strain(尽管您可能需要在Nielsennewupdated 中预先分配)。
  • 我认为您在nelements = hist(thresh_strain(:),myUniqueValues); 中遇到了错误。如果是,则意味着您不能有一个向量(在您的情况下为myuniquevalues),其中值在任何时间点都会减小。不过令人惊讶的是,您在此之前使用的是 unique,它将按升序对值进行排序。

标签: matlab function histogram montecarlo


【解决方案1】:

你的错误就在这一行(错误消息可能甚至说这么多):

nelements  = hist(thresh_strain(:),myUniqueValues);

问题在于myUniqueValues 不是“单调非递减”。此示例产生相同的错误:

n  = hist(randi(3,[1 10]),[3 2 1]);

导致

Error using histc
Edge vector must be monotonically non-decreasing.

Error in hist (line 92)
    nn = histc(y,edges,1);

我不确定这是怎么回事,因为unique 应该对其输出进行排序。所以我不能在 R2013b 中完全复制你的错误。也许您正在运行旧版本的 Matlab。我来看看myUniqueValues 的值是多少。

【讨论】:

    【解决方案2】:

    此类错误主要是由于hist函数的第二个参数不是单调不递减

    【讨论】:

      【解决方案3】:

      将您的 unique 行更改为:

      myUniqueValues = unique(roundedValues,'sorted');
      

      您可能有一个旧版本默认不排序,而是使用“稳定”排序。

      【讨论】:

      • 当前文档似乎表明这种更改的行为仅适用于多个输出情况,但可能不是。我也找不到使用 'legacy' 选项复制 2013b 中的错误的方法。
      • @chappjc Unique 总是对输出进行排序。直到现在,他们才包含'stable' 的选项。我说的对吗?
      • @Parag 老实说,我不能告诉你。我认为 horchler 是正确的,即 legacy 仅适用于索引,但是我没有任何方法可以仅检查版本。
      猜你喜欢
      • 2019-11-18
      • 2013-03-15
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      相关资源
      最近更新 更多