【问题标题】:How to transfer excel formula to octave?如何将excel公式转换为八度?
【发布时间】:2017-02-18 19:15:46
【问题描述】:

如您所见(excel 文件),我想在 Octave 中使用该公式来获得所需的结果。我还上传了八度代码图片和工作区图片。在工作区中,我的存储变量的结果/值应该与 excel(存储列)中的值相同。我怀疑在代码中使用的最后一部分(带有 i-1 的 if 语句似乎是错误)。

有人可以帮我弄清楚吗?让我知道是否需要进一步澄清。我也在下面发布我的代码:

BM_max = 1236;
virtual_feed_max = 64;
operation = dlmread ('2020Operation.csv');
BM = ones (size (operation, 1), 1);
for i=1:size(operation,1)
  if operation(i,1)==1
    BM(i,1)=BM_max;
  else
    BM(i,1)=0;
  end
end
virtual_feed = ones(size(operation,1),1);
virtual_feed(:,1) = 64;
storage = ones(size(BM,1),1);
c = ones(size(BM,1),1);
for i=1:size(BM,1)
  c=(BM(:,1)-virtual_feed(:,1));
end
for i=1:size(BM,1)
  if ((i=1)&& c)<0
    storage(:,1)=0;
  elseif ((i=1)&& c)>0 
    storage(:,1)=c;
  else
  # Issue is below (Taking the value from subsequent row is the problem) 
    if (c+(storage(i-1,1)))<0  
      storage(:,1)=0;
    elseif (c+(storage(i-1,1)))>0  
      storage(:,1)=(c+(storage(i-1,1)));
    end    
  end
end

WorkspaceExcel

【问题讨论】:

  • 我看到的只是一张图片的一个断开的链接。请将您的代码包含为文本、格式正确,而不是图像。
  • 我也包含了代码。寻找您的建议
  • 首先,((i=1)&amp;&amp; c)&lt;0 似乎是错误的,也许您的意思是((i==1)&amp;&amp; c&lt;0)?如果您将代码格式化为code block 也会更好。
  • ((i=1)&& c)
  • ((i=1)&amp;&amp;c)&lt;0 将变量i 设置为1 的值,然后执行ic 的逻辑与,它只能是真或假,并且永远不会小于零。 所有用户都可以使用我之前评论中链接中描述的代码块。

标签: matlab octave freemat


【解决方案1】:

我认为您想要的是以下内容(从您的 Excel 屏幕截图中可以看出)

BM_max = 1236;
virtual_feed_max = 64;
operation = [0; 1; 1; 1; 1; 1; 1; 1; 0; 0; 0; 0; 0];

BM = BM_max * operation;
virtual_feed = repmat (virtual_feed_max, size (operation));

storage = zeros (size (operation));
for i=2:numel (storage)
  storage (i) = max (BM(i) - virtual_feed(i) + storage(i-1), 0);
endfor

storage

哪个输出:

storage =

      0
   1172
   2344
   3516
   4688
   5860
   7032
   8204
   8140
   8076
   8012
   7948
   7884

我保留矢量化部分,以便您更快地完成。 (提示:看看cumsum

【讨论】:

    【解决方案2】:

    从现在开始

    for i=1:size(BM,1)
      if ((i=1)&& c)<0
        storage(:,1)=0;
      elseif ((i=1)&& c)>0 
        storage(:,1)=c;
      else
      # Issue is below (Taking the value from subsequent row is the problem) 
        if (c+(storage(i-1,1)))<0  
          storage(:,1)=0;
        elseif (c+(storage(i-1,1)))>0  
          storage(:,1)=(c+(storage(i-1,1)));
        end    
      end
    end
    

    您不会更改storage 中的单个值,而是更改所有行/列,因此每次迭代,所有行/列都被更改,而不是单个“单元格”。 你应该使用这样的东西:

    storage(i,1) = 0;
    

    顺便说一句,许多“for”循环可以更改为向量操作。示例:

    for i=1:size(BM,1)
      c=(BM(:,1)-virtual_feed(:,1));
    end
    

    可以更改为:

    c = BM - virtual_feed;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多