Yes, you could do that but I'm unsure if you'd really need a for-loop. If you could explain your real intentions, the actual solution maybe simpler. I'm gonna assume you know you'd need it only this way.
range = 50;
sz = size(X);
Here I've considered to be a vector(you could simply add the second index when you write it for a matrix) and I check if the k-th element is greater or equal to the biggest of the elements within the given range. I have used max and min, so we cover the range where we have less elements on either side.