2011-03-05 47 views
0

我在Matlab中很新。我做了一個for循環與我到m,和j到n。我寫了這個代碼來獲取矩陣的一個子矩陣,但它一直給我這個錯誤如何更正「下標索引必須是真正的正整數或邏輯」。在我的代碼在Matlab中?

下標索引必須是真正的正整數或邏輯。 這是代碼

for i=1:m, 
for j = 1:n, 
    display(i); 
    display(j); 

      edgeil = 2; 
    edgeib = 2; 
    edgejb = 2; 
    edgejl = 2; 


    if((i-CenteriSE)< 0) 
     edgeib = CenteriSE - (-1)*(i-CenteriSE); 

    end 
    if((i+ CenteriSE)> m) 
     temp = i+ CenteriSE - m; 
     edgeil = CenteriSE - temp; 
    end 

    if((j-CenterjSE)< 0) 
     edgejb = CenterjSE- (-1)*(j-CenterjSE); 

    end 
    if((j+ CenterjSE)> n) 
     temp2 = j+ CenterjSE - n; 
     edgejl = CenterjSE - temp2; 
    end 

    bok1 = round(edgeib); 
    bok2 = round(edgeil); 
    bok3 = round(edgejb); 
    bok4 = round(edgejl); 
    display(bok1); 
    display(bok2); 
    if((bok1 == round(bok1)) && (bok2 == round(bok2)) && (bok3 == round(bok3)) && (bok4 == round(bok4))) 
    B = circles(i-round(bok1):i+round(bok2),j-round(bok3):j+round(bok4)); 
    end 

我寫的if語句和輪s到糾正它,但它不工作。請幫我解決這個問題?

+0

CenteriSE和CenterjSE都是2.它不會超出範圍。 – Ada 2011-03-05 21:33:26

+0

我也使用abs(..),但我不知道爲什麼id一直給我這個錯誤。請幫助一下? – Ada 2011-03-05 21:39:15

+0

它說什麼線?你有沒有試過圓圈(round(i-bok1):round(i + bok2),round(j-bok3):round(j + bok4));'? (這裏可能有一個off-by-one ......) – rlibby 2011-03-05 22:01:49

回答

0

好吧,很簡單。首先讓我們刪除所有的混亂。你說CenteriSE = 2,所以這個聲明

edgeib = CenteriSE - (-1)*(i-CenteriSE);相當於edgeib=ii=1

現在,如果你去你最後的聲明B = circles(i-round(bok1):i+round(bok2),j-round(bok3):j+round(bok4));,你在做i-round(bok1),這只是i-i=0i=1。 Matlab的索引從1開始,這就是你得到這個錯誤的原因。

+0

是的,謝謝你在我檢查了我的等式之後,我意識到了。我沒有一個很好的數學:) – Ada 2011-03-06 15:38:39

相關問題