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:

»>⁴¬×» – Dyadic (2-argument) link.
»      – 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 Jul 19, 2018 at 15:50
• @Giuseppe tx! I was able to save two more :) Jul 19, 2018 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.

c&s|x<-[1..c]=[sum[j|j<=s]|j<-x>>=(<$>x).max]  Try it online! Explanation The term x >>= (<$> x) . max is a golfed version of

concat [ max i <$> x | i <- x ]  which evaluates to [1,2,3,4..c, 2,2,3,4..c, 3,3,3,4..c, ..., c,c,c,c..c]. Now we only need to force the values to 0 once they exceed s which we achieve with sum [ j | j <= s]. APL(Dyalog Classic), 12 bytes {⍺ ⍺↑∘.⌈⍨⍳⍵}  Try it online! Any tips on turning this into a train are welocome. • My train is longer by 2 Jul 19, 2018 at 18:06 • 13 bytes Jul 19, 2018 at 18:14 APL (Dyalog), 12 bytes o×⎕≥o←∘.⌈⍨⍳⎕  Try it online! • Would something like o×⎕≥o←∘.⌈⍨⍳ be allowed, or would you have to assign it to a function in order for that to count? Jul 21, 2018 at 16:05 • @Zacharý my guess is that one would need to be put it inside a tradfn with an argument or a dfns Jul 21, 2018 at 19:17 JavaScript (ES6), 61 bytes Takes input in currying syntax (c)(s), where s is 1-indexed. Returns a 1-dimensional array. c=>s=>[...Array(c*c)].map((_,k)=>(k=k%c>k/c?k%c:k/c)<s?-~k:0)  Try it online! Jelly, 6 bytes ⁴Ri»µþ  A full program* taking integers C and S which prints the Jelly representation of a list of lists of integers as defined (1-indexed). Try it online! (formats the result of the dyad as a grid of numbers for easier reading) How? ⁴Ri»µþ - Main Link: C, S þ - outer product with: µ - the monadic function (i.e. f(x,y) for x in [1..C] for y in [1..C]): » - maximum (of x and y) ⁴ - program's 4th argument = 2nd input = S R - range = [1,2,3,...S] i - first index of (the maximum) in (the range) or 0 if not found - as a full program: implicit print  * The reason this is a full program is down to the use of the program argument access, ⁴. As a dyadic link this code would rely on how the program which is using it is called. Reusable dyadic link in 8 bytes (taking S on the left and C on the right): RiⱮⱮ»þ} Reusable dyadic link in 8 bytes (taking C on the left and S on the right): RiⱮⱮ⁹»þ¤ Java 10, 88 bytes C->S->{var r=new int[C][C];for(;S>0;)for(int s=S--;s-->0;)r[S][s]=r[s][S]=S+1;return r;}  Try it online. Explanation: C->S->{ // Method with two int parameters and int-matrix return-type var r=new int[C][C]; // Result-matrix of size C by C for(;S>0;) // Loop as long as S is not 0 yet: for(int s=S--;s-->0;) // Inner loop s in the range (S, 0] // (and decrease S by 1 in the process with S--) r[S][s]=r[s][S]=S+1; // Set the values at both {S,s} and {s,S} to S+1 return r;} // Return the result  PHP, 92 bytes This is "1-indexing". <?list(,$c,$s)=$argv;for(;$i++<$c;print"\n")for($j=0;$j++<$c;)echo$s<$i||$s<$j?0:max($i,$j);  To run it: php -n <filename> <c> <s>  Example: php -n collapsing_matrice.php 8 6  Stax, 10 bytes ▓╜.→,cΘ○╤æ  Run and debug it How it works: R(Xm]i*xit+J Full program, implicit input. R 1-based range of S ( Right-pad with zeroes to length C X Save to X register m Map (same as here): ] Wrap in list i* repeat by iteration index xit Remove first elements from X register + Append J Stringify each element, and join by space  Excel VBA, 65 bytes An immediate window function that takes input from [A1:B1] and outputs to the range [C1].Resize([A1],[A1]). [C1].Resize([A1],[A1])=0:For s=-[B1]To-1:[C1].Resize(-s,-s)=-s:Next  Input / Output Input is in range [A1:B1] J, 18 bytes ,~@[{.[:>./~1+i.@]  Much longer than both APL solutions. Try it online! MATLAB, 58 bytes (Thanks to anonymous user) function o=f(c,s);o=zeros(c);for j=s:-1:1;o(1:j,1:j)=j;end  Just filling the elements of matrix with appropriate number, running a loop. Maybe possible to be cleverer with arrayfun • You don't need to name the function and you can use zeros(c) which safes some bytes. Also did you see this Octave answer, I guess it would work in Matlab too? Jul 20, 2018 at 0:48 • @OMᗺ Octave you can't name variables inside anonymous functions in matlab. Also, max() have to take same-shape arguments Jul 20, 2018 at 0:56 • An anonymous user suggested function o=f(c,s);o=zeros(c);for j=s:-1:1;o(1:s,1:s)=j;end. Jul 20, 2018 at 12:11 • @JonathanFrech oh my so much simpler :-( just need to be o(1:j,1:j)=j Jul 20, 2018 at 19:18 C# (.NET Core), 85 bytes c=>s=>{var r=new int[c,c];for(;s>0;)for(int j=s--;j-->0;)r[s,j]=r[j,s]=s+1;return r;}  Try it online! A port of Kevin Cruijssen's answer, which was much better than mine. Python 2, 58 bytes lambda C,S:[-~max(i%C,i/C)*(i%C<S>i/C)for i in range(C*C)]  Try it online! Outputs a 1D list of length C*C. Charcoal, 19 bytes Ｅθ⪫ＩＥＥθ⌈⟦ιλ⟧∧‹λη⊕λ  Try it online! Link is to verbose version of code. 3 bytes used to convert the output to decimal and format it nicely. Explanation:  θ Input C Ｅ Map over implicit range θ Input C Ｅ Map over implicit range λ Inner index ι Outer index ⌈⟦ ⟧ Maximium Ｅ Map over results λ Current value η Input S ‹ Less than λ Current value ⊕ Incremented ∧ Logical AND Ｉ Cast to string ⪫ Join with spaces Implicitly print on separate lines  Clean, 67 bytes import StdEnv$n s=[[if(i>s||j>s)0(max i j)\\i<-[1..n]]\\j<-[1..n]]


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. Jul 31, 2018 at 13:39

Husk, 11 bytes

T0:´ṪYḣ⁰R0


Try it online! The function takes S followed by C.