Lev - there are a couple of problems with the above - your condition will fail for chek equal to one since chek-1 will be invalid. Likewise, when chek is the length of a, then chek+1 will exceed the array dimensions. So you need to handle both of these cases. Also, g is being used in your condition when you probably mean p...but then you should probably be using length(p) since you are iterating over this array and not a.
Your code could then become
p = [154 162 1190 153 144 142];
o = zeros(size(p));
for k = 1:length(p)
if k == 1
if p(k) < p(k+1) +20
o(k) = p(k);
end
elseif k == length(p)
if p(k) < p(k-1) + 20
o(k) = p(k);
end
elseif p(k) < p(k-1) + 20 && p(k) < p(k+1) + 20
o(k) = p(k);
end
end
There are probably different ways that you can do the above (perhaps even removing the for loop) but I've tried to keep it similar to what you had already...which is very close to already doing what you wanted.