MATLAB: Replacing numbers around a specific index(CAN’T FIGURE IT OUT)

array manipulationcolumn-orderindexingMATLABmatrix manipulationrow-order

I am trying to create a sub-function for a EEG analysis program. Basically the data is a 1×892 matrix of data with numbers ranging from 1 to 99. This portion that i am trying to deal with has two parts:
1. Go through the data and find the value 9, whenever it occurs in the 1×892 matrix. I have already done this part.
2. Replace the numbers surrounding the identified number 9. So if there was data with 9 in it follows, [… a b c d 9 e f g h i j k…]. Basically after the part of identifying the number 9, the letters a through d need to be replaced with 5,6,7,8 respectively. The letters e through k need to be replaced with 10,11,12,13,14,15,16, respectively. I am struck on this part and i would really appreciate any help.
P.s, the way this data is arranged, that we get from an EEG, a 9 will not overlap with another 9.

Best Answer

This seems to work:
x = randi(99, 1, 200); % Create Data
ix9 = find(x == 9);
for k1 = 1:length(ix9)
if (ix9(k1)+7) <= length(x) && (ix9(k1)-4) >= 1 % Check For Out-Of-Range Indices
ixrng = ix9(k1) + [-4:7]; % Calculate Index Range
x(ixrng) = 5:16; % Substitute
end
end