登录论坛

查看完整版本 : 如何更正“下标索引必须是实数正整数或逻辑”。我的代码在Matlab中?


poster
2019-12-14, 20:13
我是Matlab的新手。我做了一个for循环,其中i到m,j到n。我写了这段代码来接受矩阵的子矩阵,但它一直给我这个错误

下标索引必须是实数正整数或逻辑值。这是代码

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来纠正它,但是它不起作用。请帮我如何解决这个问题?



回答:

好吧,这很简单。首先,让我们消除所有混乱。你说CenteriSE = 2,所以这句话

edgeib = CenteriSE - (-1)*(i-CenteriSE);等价于i=1 edgeib=i 。

现在,如果您转到最后一条语句,则B = circles(i-round(bok1):i+round(bok2),j-round(bok3):j+round(bok4)); ,您正在做i-round(bok1) ,当i=1时, ii=0 。 Matlab的索引从1开始,这就是为什么会出现此错误的原因。



更多&回答... (https://stackoverflow.com/questions/5206910)