2017-02-16 38 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;