Info

この質問は閉じられています。 編集または回答するには再度開いてください。

horizontal Transition code error index out of bounds anyone help!!!

1 回表示 (過去 30 日間)
priya
priya 2012 年 5 月 24 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
horizontal transition:
horizontal scanning thro block and finds no of times pixel value change state from 0 to 1 or 1 to 0
my coding :
[r c]=size(g);
for i=1:r-1
for j=1:c-1;
if(j==c)
break;
elseif g(i,j)==0 && g(i,j+1)==1
c=c+1;
end
end
end
disp(c);
and error is
Attempted to access g(213,1118); index out of bounds because size(g)=[1537,1117].
Error in ==> ht at 8
elseif g(i,j)==0 && g(i,j+1)==1
can anyone help to solve dis pls?

回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 5 月 24 日
You could encounter that error if g has more than 2 dimensions.
By definition, if you use two output arguments, then
[r c] = size(g)
would be the same as
t = size(g);
r = t(1);
c = prod(t(2:end))
If for example, you had a 10 x 10 x 3 g matrix, then [r c] = size(g) would be r=10 and c=10*3
Of course other parts of your code are likely to have logical problems if g has more than 2 dimensions, but this size() problem is the way that you end up with the second dimension being out of range.
  2 件のコメント
priya
priya 2012 年 5 月 24 日
g is binary image so tats not a prob ,size of image is 1117x1573. wen i perform j+1 in elseif statement , j taking value 1118(index out of bound) i ve to stop wen j is 1118 tat only my prob
Walter Roberson
Walter Roberson 2012 年 5 月 24 日
Please show us the values of r and c as known to your program (that is, put in a breakpoint and see what the _program_ thinks they are.)

priya
priya 2012 年 5 月 25 日
im=imread('');
g=rgb2gray(im);
[r c]=size(g);
c1=0;
for i=1:r-1
for j=1:c-1
if g(i,j)==0 && g(i,j+1)==1
c1=c1+1;
end
end
end
disp(c1);
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 5 月 25 日
That code would miss the last row, i==r .

Community Treasure Hunt

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

Start Hunting!

Translated by