Well A+ for effort! But there is a way more efficient method for doing this. You need sparse matrices. Try something like
n = 20;
e = ones(n^2,1);
o = e;
for i = n:n:n^2-1
o(i) = 0;
end
A = spdiags([-e -o 4*e -o -e], [-(n+1) -1 0 1 (n+1)], n^2, n^2);
if you really want to generate it yourself. I post this method so you can learn how to make banded sparse matrices using general pracitce. But for a Poisson matrix you can simply use the built in MATLAB one:
B = gallery('poisson',n);
To illustrate why you need sparse matrices, try checking the sparsity for various values of n with
sparsity = nnz(B)/prod(size(B));
Increasing the number n to somewhere around 20 is when poisson matrices really make a difference as they really are sparse (close to 1% are nonzeros). All these zeros in MATLAB are wasted space. So when you are generating B by your full for-loop method you are racking up memory! To see the difference try on your original code something like
sB = whos('B');
sC = whos('C');
disp(sA.bytes);
disp(sB.bytes);
to see that for n = 20 you get B = 1280000 bytes and C = 33928 bytes. But using this prescribed method then A = 33896 bytes! That's a difference of nearly 1.2 MB!