Hello Amirhosein,
I noticed that the function sList does not work properly. To see this run the code above with n = 5 and
look at
sList[#, n] & /@ pos;
%//MatrixForm
You see that in the 3rd row the position of the spike at 12 is missing, that is correct, but there are not 5 positions at the left and right side as it was required. Therefore I wrote an alternative. Use this, but make sure that you define
bL = Length[bigmat];
pL = Length[pos];
as soon as bigmat and pos are known. The new function is
Clear[sList1]
sList1[kk_, nk_] := Module[{nn},
nn = nk;
Which[
kk == 1,
t1 = Complement[Table[j, {j, 2, 2 + nn + pL}], pos];
t1L = Length[t1];
If[t1L > nn, t1 = Drop[t1, nn - t1L]];
t1,
kk == bL,
t1 = Complement[Table[j, {j, bL - 1 - nn - pL, bL - 1}], pos];
t1L = Length[t1];
If[t1L > nn, t1 = Drop[t1, nn - t1L]];
t1,
1 < kk <= nn,
t1 = Complement[Table[Max[j, 1], {j, kk - 1 - nn - pL, kk - 1}],
pos];
t1L = Length[t1];
t2 = Complement[Table[j, {j, kk + 1, kk + 1 + nn + pL}], pos];
t2L = Length[t2];
If[t1L + t2L > 2 nn, t2 = Drop[t2, 2 nn - t1L - t2L]];
Join[{t1, t2}] // Flatten,
bL - nn <= kk < bL,
t1 = Complement[Table[j, {j, kk - 1 - nn - pL, kk - 1}], pos];
t1L = Length[t1];
t2 = Complement[Table[Min[j, bL], {j, kk + 1, kk + 1 + nn + pL}],
pos];
t2L = Length[t2];
t1 = If[t1L + t2L > 2 nn, t1 = Drop[t1, t1L + t2L - 2 nn]];
Join[{t1, t2}] // Flatten,
True,
t1 = Complement[Table[j, {j, kk - 1 - nn - pL, kk - 1}], pos];
t1L = Length[t1];
t2 = Complement[Table[j, {j, kk + 1, kk + 1 + nn + pL}], pos];
t2L = Length[t2];
If[t1L + t2L > 2 nn, t1 = Drop[t1, t1L - nn];
t2 = Drop[t2, nn - t2L]];
Join[{t1, t2}] // Flatten
]
]
With this try
pos
sList1[#, 5] & /@ pos;
% // MatrixForm