MATLAB: Plz help me correct the last loop…

super long complicated expressions

N=10;
h=(1-0)/(1+N);
x=0:h:1;
y=0:h:1;
A=diag(-4*ones(1,N))+diag(ones(1,N-1),1)+diag(ones(1,N-1),-1);
M=zeros(N^2,N^2);
for i=1:N
M((i-1)*N+1:i*N,(i-1)*N+1:i*N)=A;
end
for i=1:N-1
M((i-1)*N+1:i*N,(i)*N+1:(i+1)*N)=eye(N);
end
for i=1:N-1
M((i)*N+1:(i+1)*N,(i-1)*N+1:i*N)=eye(N);
end
[xx,yy] = meshgrid(x,y);
f= -100.*xx .*yy;
F=reshape(f,(N+2)^2,1);
solutions=zeros((N+2)^2,1);
for i=1:N
for j=1:N
solutions((j-1)*(N+2)+2:(j-1)*(N+2)+N+1)=h^2*M((i-1)*(N+1)+1:(i-1)*N+N,(j-1)*(N+1)+1:(j-1)*N+N)\F((j-1)*(N+2)+2:(j-1)*(N+2)+N+1);
end
end

Best Answer

When i = 1, j = 2, then
h^2*M((i-1)*(N+1)+1:(i-1)*N+N,(j-1)*(N+1)+1:(j-1)*N+N)
is 10 x 9, and
F((j-1)*(N+2)+2:(j-1)*(N+2)+N+1)
is 10 x 1.
You have the \ operator between the parts. When you use the \ operator, the size of the output of A\B is size(A,2) by size(B,2), so with (10 x 9) and (10 x 1) you are going to get a 9 x 1 result.
You are assigning that to
solutions( (j-1)*(N+2)+2:(j-1)*(N+2)+N+1 )
which is a vector of 10 values.
I cannot make any suggestions about what the correct code should be, as you have carefully stored all of the comments in a secret location in case anyone got a-hold of the code and wanted to be sure that they would not be able to understand what your code was for.