The end keyword (when used in indexing) is converted to the size of that dimension.
%example:
A=rand(1,2,5,3);
A(1,1,end,1)=2;%end is replaced by size(A,3), so with 5
This also works in other calls:
%example:
A=rand(1,2,5,3);
A(1,min(end,7),2,2)=-1;%here end is replaced by the value 2 **before** the call to min
So in your code end+1 means that you are assigning a value to the next position, which automatically extends the array to fit that size.
NB: I disagree with using i and j as variables here, as well as the lack of any comments whatsoever. You see that a lack of comments causes confusion.