index out of bounds

35 ビュー (過去 30 日間)
med-sweng
med-sweng 2014 年 2 月 27 日
回答済み: Andy 2014 年 2 月 27 日
I have passed a matrix "p" to some function. In that function, I want to check the four neighbourhoods of some pixel.
For that, I got the following error:
Attempted to access p(1,51); index out of bounds because size(p)=[50,50].
How can I workaround this issue?
Thanks.

回答 (3 件)

Mischa Kim
Mischa Kim 2014 年 2 月 27 日
編集済み: Mischa Kim 2014 年 2 月 27 日
As the message implies you are trying to access an element that does not exist. Pixel (1,50) only has 3 neighbours.
... (1,48) (1,49) (1,50)
... (2,48) (2,49) (2,50)
... (3,48) (3,49) (3,50)
The solution would be to define the neighbourhood of a pixel depending on its location. If that approach allows you to even consistently do the comparision, that is.
  2 件のコメント
med-sweng
med-sweng 2014 年 2 月 27 日
Can we for instance modify the size of the array to be 51x51 for instance, by padding the additional elements with "0"?
Mischa Kim
Mischa Kim 2014 年 2 月 27 日
Sure, you could. Again, I believe the question is, whether or not your analysis is still valid, since you do analysis on pixels with different number of neighbours. You'd be the only one that can make that call.
As an alternative you could limit your analysis to pixels excluding the outermost ones.

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


the cyclist
the cyclist 2014 年 2 月 27 日
What do you mean be work around?
When you are at the edge of your array (index=50), you have to put something in your code to define the "neighborhood" differently, because you cannot access an element at index=51. (That element does not exist in a 50x50 array.)
  1 件のコメント
med-sweng
med-sweng 2014 年 2 月 27 日
Can we for instance modify the size of the array to be 51x51 for instance, by padding the additional elements with "0"?

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


Andy
Andy 2014 年 2 月 27 日
I assume you have two variables which you are indexing to loop through the image i.e.
for x = 1:50
for y = 1:50
% Code to check condition of neighbouring pixel above
% Code to check condition of neighbouring pixel below
% Code to check condition of neighbouring pixel left
% Code to check condition of neighbouring pixel right
end
end
The first part of the code in each section should be to check if the value of X±1 and Y±1 is still valid. i.e For the first check
if (((x+1)<51)&&((y+1)<51))
% code to check pixel x+1,y+1
else
% return dummy value or ignore
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by