Collapsing Matrices

Related: Let's design a digit mosaic, Print/Output the L-phabet. Sandbox post here

Given 2 inputs C = columns and rows, S = starting point output a matrix as follow:

Input 4, 3

1   2   3   0
2   2   3   0
3   3   3   0
0   0   0   0

Explanation

Given C = 4, S = 3

1) Create a C x C matrix filled with 0

4 columns
4     _____|____
|          |
r  --0  0   0   0
o |  0  0   0   0
w |  0  0   0   0
s  --0  0   0   0

2) Fill with S values within row and column S, then subtract 1 from S and repeat until S = 0. This case S = 3

Column 3
S = 3           |
v
0   0   3   0
0   0   3   0
Row 3-->3   3   3   0
0   0   0   0

Column 2
S = 2       |
v
0   2   3   0
Row 2-->2   2   3   0
3   3   3   0
0   0   0   0

Column 1
S=1     |
v
Row 1-->1   2   3   0
2   2   3   0
3   3   3   0
0   0   0   0

Final Result

1   2   3   0
2   2   3   0
3   3   3   0
0   0   0   0

Rules

• Assume C >= S >= 0
• The output can be a matrix, list of lists, array (1-dimensional or 2-dimensional) etc.
• You can take inputs via any default I/O format
• Your program, function, etc... may be 1-indexing or 0-indexing. Please specify which one is.

Note Explanation is 1-indexing

Winning criteria

Jelly, 8 bytes

»>⁴¬×»µþ

Try it online!

How it works

Jelly's Outer Product Atom (þ)

You can think of Jelly's outer product atom, þ, as a quick (operator) that, given integer arguments $X$ and $Y$ (in this case $X=Y=\text{first argument }$), produces the following matrix of tuples:

$$\left[\begin{matrix} (1, 1) & (2, 1) & (3, 1) & \cdots & (X, 1) \\ (1, 2) & (2, 2) & (3, 2) & \cdots & (X, 2) \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ (1, Y) & (2, Y) & (3, Y) & \cdots & (X, Y) \end{matrix}\right]$$

It also applies the link right before it to all pairs, let's call it $\:f$, which behaves like a function which takes two arguments, producing something like this:

$$\left[\begin{matrix} f(1, 1) & f(2, 1) & f(3, 1) & \cdots & f(X, 1) \\ f(1, 2) & f(2, 2) & f(3, 2) & \cdots & f(X, 2) \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ f(1, Y) & f(2, Y) & f(3, Y) & \cdots & f(X, Y) \end{matrix}\right]$$

How is it relevant to the task at hand?

This works by noticing that every value in the expected output is just a table of maximal indices, or $0$ if this maximum exceeds our second argument. Therefore, we can create the following link to perform this mapping:

»      – Maximum of the X, Y coordinates.
>⁴    – Check if this exceeds the second argument of the program.
¬   – Negate this boolean.
×» – And multiply by the maximum, computed again.

R, 47 41 bytes

function(C,S,m=outer(1:C,1:C,pmax))m*!m>S

Try it online!

1-indexed. Generates the outputs for S==C (no zeros) then zeroes cells which have a value >S using matrix multiplication (thanks Giuseppe for 4 bytes!).

• Neat! multiplication will get you some good mileage: 43 bytes – Giuseppe Jul 19 '18 at 15:50
• @Giuseppe tx! I was able to save two more :) – JayCe Jul 19 '18 at 15:55

Octave, 31 bytes

@(C,S)(u=max(t=1:C,t')).*(u<=S)

Anonymous function that returns a matrix. Uses 1-based indexing.

Try it online!

Haskell, 47 45 bytes

-2 bytes by changing the output format to one-dimensional list.

Try it online!

Defines $:: Int Int -> [[Int]] giving an answer using 1-based indexing. Perl 6, 37 bytes {((^$^c+1 Xmax^$c+1)Xmin$^s+1)X%\$s+1}

Try it online!

Returns the matrix as 1-dimensional array.

Mathematica 44 bytes

Table[If[i <= s && j <= s, Max[i, j], 0], {i, c}, {j, c}]
• Are you certain the whitespace is necessary? I can't test Mathematica but I don't think it is. – Sriotchilism O'Zaic Jul 31 '18 at 13:39