Attempted to access b(0,:); index must be a positive integer or logical.

1 回表示 (過去 30 日間)
Andy
Andy 2013 年 4 月 17 日
for k=1:14;
[col,row]=size(b);
prj=zeros(col,1);
for i=1:col
for j=1:row
if b(i,j)==0
prj(i)=prj(i)+1;
end
end
end
rup = 0;rdw = 0;
for i = 1:col
if prj(i)==0 && prj(i+1)>=1
rup = i;
end
if prj(i)>=1 && prj(i+1)==0
rdw = i+1;
break
end
end
range = rdw-rup;
ab = ones(range,row);
for i= rup:rdw
ab(i-rup+1,:) = b(i,:);
b(i,:)=1;
end
figure;imshow(ab);
k=k+1;
end
I have some problems with the above code and I keep getting the "attempt to access" error.
The size of matrix b is [3508 2480].
Can anybody shed some like as to fix it?
I get the following error:
Attempted to access prj(3509); index out of bounds
because numel(prj)=3508.
Error in main (line 67)
if prj(i)==0 && prj(i+1)>=1

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 4 月 17 日
You start with rup = 0. You then have a loop that conditionally assigns a different value to rup. You then loop "i" starting from rup. But suppose the conditions of the conditional assignment were never true: then rup would still be 0, and you would be looping starting from 0.

Andy
Andy 2013 年 4 月 17 日
Thank you for your explanation. It seems really complicated to understand. Are there anything that I can change to make it work? Like plus 1 somewhere? Thanks!
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 4 月 17 日
You need to decide what you want to do if your "for i = 1:col" loop does not find any matches.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by