You certainly don't need a loop to generate your a.
a = ones(n) * 8;
[rows, cols] = ndgrid(1:n);
a(rows == 1 | rows == cols) = 4;
Note that because your i and j variables are meaningless, it's not clear whether you meant it to be column 1 or row 1 that get sets to 4. I assume it was rows but it's obvious how to change it to columns. Using variable names with meaning makes the code much clearer. Moreover i and j are actually functions in matlab, so are not recommended as variable names anyway.
Best Answer