2017-02-16 35 views
0

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

有人可以帮我弄明白吗?让我知道是否需要进一步澄清。我也是张贴我的代码如下太:

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

+1

我看到的只是一个断开的链接到一个图像。请将您的代码添加为文本,格式正确,而不是图片。 – beaker

+0

我也包含了代码。寻找你的建议 – Mayil

+0

首先,'((i = 1)&& c)<0'似乎是错的,也许你的意思是'((i == 1)&& c <0)'?如果您将代码格式化为[代码块](http://stackoverflow.com/help/formatting),那也会更好。 – beaker

回答

0

我想你想要的是以下(从您的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

0

从这点上

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; 

BTW,很多那些“为”循环可以更改为向量运算。例如:

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

可以更改:

c = BM - virtual_feed;