小编mar*_*l47的帖子

没有for循环,有没有办法做到这一点?

目前我在MATLAB中有这个功能

function [ y ] = pyramid( x )
%PYRAMID Returns a "pyramid"-shapped matrix.
y = zeros(x); % Creates an empty matrix of x by x.
rings = ceil(x/2); % Compute number of "rings".
for n = 1:rings
    % Take the first and last row of the ring and set values to n.
    y([n,x-n+1],n:x-n+1) = n*ones(2,x-2*(n-1));
    % Take the first and last column of the ring and set values to n.
    y(n:x-n+1,[n,x-n+1]) = n*ones(x-2*(n-1),2);
end
end
Run Code Online (Sandbox Code Playgroud)

其中产生以下输出:

piramide(4)
ans = …
Run Code Online (Sandbox Code Playgroud)

matlab for-loop function matrix

1
推荐指数
1
解决办法
68
查看次数

标签 统计

for-loop ×1

function ×1

matlab ×1

matrix ×1