- make sure that X, Y, i and j are integers
- make sure that X > i and Y > j
- and, you never know, make sure that you haven't got a variable called sum that stops you from using the sum function.

1 view (last 30 days)

Guillaume
on 4 Apr 2017

The problem you have is not that your subscripts are not logical, it is that either they're not positive or they're not integers. I'm fairly certain, even with the very limited info you've provided, that converting to logical won't give you the result you want.

So,

- make sure that X, Y, i and j are integers
- make sure that X > i and Y > j
- and, you never know, make sure that you haven't got a variable called sum that stops you from using the sum function.

And on that last point, don't use mean as a variable name since it prevents you from using the mean function.

Guillaume
on 5 Apr 2017

Well, yes that code is not going to work. X(1) is 1, so for any i, the expression X-i is always going to result in at least one value less than 1. When i is 1224, none of the values in X-i are valid indices.

In any case, nothing in that loop makes sense and I've no idea what you're trying to achieve. If you're trying to find the mean of the image over a moving window then you can use conv2:

%mean of image over sliding window of size mxn:

conv2(img, ones(m, n)/(m*n))

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/333508-problem-in-calculation-formula#comment_442798

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/333508-problem-in-calculation-formula#comment_442798

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/333508-problem-in-calculation-formula#comment_443113

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/333508-problem-in-calculation-formula#comment_443113

Sign in to comment.