这是一种没有循环的方法:
m = 5; %// number of rows
n = 12; %// number of cols
c = ' '; %// inner character
C = '*'; %// outer character
x = repmat(c, m, n); %// create m x n array of c
x([1 end], :) = C; %// replace top and bottom with C
x(:, [1 end]) = C; %// replace left and right with C
Run Code Online (Sandbox Code Playgroud)
这给了
>> disp(x)
************
* *
* *
* *
************
Run Code Online (Sandbox Code Playgroud)