# Drawing a pyramid in MATLAB

I want to draw this figure in Matlab (no bubbles)!

I wrote the following code:

```
figure
hold on
axis equal
axis([0 20 0 10])
n = 20
n = n - 1
for y = 0:10
for x = (y+1):n
rectangle('Position',[x y 1 1],'curvature',[0 0],'facecolor',(rand(1,3)))
pause(0.05)
end
end
```

I get the following code for executing this code:

I need help writing a piece of code where the correct number can be drawn.

source to share

Your loop `x`

does not start at the right point, but goes up to the maximum at each iteration.

Just change the definition of the loop `for x = (y+1):n`

to `for x = (y+1):n-y`

and you get the desired result:

```
for y = 0:10
for x = (y+1):n-y
rectangle('Position',[x y 1 1],'curvature',[0 0],'facecolor',(rand(1,3)))
pause(0.05)
end
end
```

edit: as per your comment you wanted to achieve this by manipulating `n`

, this is also possible, but you have to decrease `n`

on each iteration of the outer loop, e.g .:

```
for y = 0:10
for x = (y+1):n
rectangle('Position',[x y 1 1],'curvature',[0 0],'facecolor',(rand(1,3)))
pause(0.001)
end
n=n-1 ;
end
```

source to share