Break it down into smaller parts
whos Sd [rows, columns] = size(Sd) index1 = PatchSizeHalf+WindowSizeHalf+1 index2 = Height+WindowSizeHalf+PatchSizeHalf index3 = PatchSizeHalf+WindowSizeHalf+1 index4 = Width+WindowSizeHalf+PatchSizeHalf
Now, is index2 bigger than rows, or index4 bigger than columns?