Subscript indices must either be real positive integers or logicals.

3 ビュー (過去 30 日間)
med-sweng
med-sweng 2015 年 2 月 8 日
回答済み: Image Analyst 2015 年 2 月 8 日
I get returned from a function I call the "pixel location".
At that location, what I try to do is:
A(pixel_location(1),pixel_location(2))=1;
But, I get the following error:
Subscript indices must either be real positive integers or logicals.
Why is that? How can I solve this issue?

回答 (3 件)

Star Strider
Star Strider 2015 年 2 月 8 日
Your ‘pixel_location’ vector likely contains values that are not integers greater than 0. Negative values, non-integer values, and zero are not valid subscripts in MATLAB array indexing.
  2 件のコメント
med-sweng
med-sweng 2015 年 2 月 8 日
Thanks for your reply. Would adding a conditional statement to check for such values solve the issue?
Star Strider
Star Strider 2015 年 2 月 8 日
A conditional statement might be able to check to be certain that particular pairs from ‘pixel_location’ are positive integers, but what does that do to your code?
I would see what ‘pixel_location’ contains, and correct it if possible. For instance if it contains integers with negative values or zero, adding a positive offset to be the minimum of ‘pixel_location’+1 would correct the problem. (You may have to make appropriate changes elsewhere in your code to account for that.) If it contains non-integer values, you would have to make other changes (perhaps scaling and rounding) to make them work as indices.
Since I don’t know what ‘pixel_location’ contains, I cannot be more specific than that.

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


Erik S.
Erik S. 2015 年 2 月 8 日
Check the values of pixel_location(1) and pixel_location(2). I guess you find either the value 0 or a decimal value. Indexes have to be integers.
  4 件のコメント
med-sweng
med-sweng 2015 年 2 月 8 日
I have an iteration, for each iteration they will have different values.
Erik S.
Erik S. 2015 年 2 月 8 日
If you can formulate a conditional statement which is updated each iteration as well it could work. But if you don't know which values are correct for pixel_location in each iteration it seems difficult.

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


Image Analyst
Image Analyst 2015 年 2 月 8 日
Try to round the values (if they might be fractional values) and clip the values to between your image limits:
[rows, columns, numberOfColorChannels] = size(yourImage);
pixel_location = round(pixel_location) % Round.
pixel_location(1) = min(pixel_location(1), rows); % Don't allow more than rows.
pixel_location(1) = max(pixel_location(1), 1); % Don't allow less than 1.
pixel_location(2) = min(pixel_location(2), columns); % Don't allow more than columns.
pixel_location(2) = max(pixel_location(2), 1); % Don't allow less than 1.

カテゴリ

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