n=3; B=4*diag(ones(1,n))-diag(ones(1,n-1),1)-diag(ones(1,n-1),-1) O=zeros(n,n) mI=-eye(n,n)It is easy to build the matrix for a small n. In our case n=3:
>> [B mI O;mI B O;O mI B] ans = 4 -1 0 -1 0 0 0 0 0 -1 4 -1 0 -1 0 0 0 0 0 -1 4 0 0 -1 0 0 0 -1 0 0 4 -1 0 0 0 0 0 -1 0 -1 4 -1 0 0 0 0 0 -1 0 -1 4 0 0 0 0 0 0 -1 0 0 4 -1 0 0 0 0 0 -1 0 -1 4 -1 0 0 0 0 0 -1 0 -1 4Let's return to this approach later in the general case. Actually, just see here
m=4;n=m-1;N=n^2 maind=4*ones(N,1) neardiag=-ones(N-1,1); fardiag=-ones(N-n,1)See the matrices separately
diag(maind) diag(neardiag,1) diag(neardiag,-1) diag(fardiag,n) diag(fardiag,-n)Now just include +... to add the above matrices.diag(maind)+... diag(neardiag,1) +... diag(neardiag,-1) + ... diag(fardiag,n) +... diag(fardiag,-n)It's good, except you need to have 0 at each nth position of neardiag.An elegant way is to form the vector [1,2,3, ...,N-1] ie. 1:N-1
Then form the remainder in division by n. Each nth gives a zero.rem(1:N-1,n) neardiag=rem(1:N-1,n)~=0 % Hence this is the right idiom, isn't it!After this adjustment just repeat the above commands.A=... diag(maind)+... diag(neardiag,1) +... diag(neardiag,-1) + ... diag(fardiag,n) +... diag(fardiag,-n)Note: The above idiom is my no means the only (elegant) solution, another idea would be like this:[ones(n-1,n);zeros(1,n)] % ans(:) % columnwise "ravel" into a long vectorThis was a bit more than was asked in exercise 1.