Collapsing Matrices

18

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

Luis felipe De jesus Munoz

Posted 2018-07-19T13:37:37.113

Reputation: 9 639

Answers

6

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!).

JayCe

Posted 2018-07-19T13:37:37.113

Reputation: 2 655

Neat! multiplication will get you some good mileage: 43 bytes

– Giuseppe – 2018-07-19T15:50:09.227

@Giuseppe tx! I was able to save two more :) – JayCe – 2018-07-19T15:55:54.043

6

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.

Mr. Xcoder

Posted 2018-07-19T13:37:37.113

Reputation: 39 774

5

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!

Luis Mendo

Posted 2018-07-19T13:37:37.113

Reputation: 87 464

5

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].

ბიმო

Posted 2018-07-19T13:37:37.113

Reputation: 15 345

5

APL(Dyalog Classic), 12 bytes

{⍺ ⍺↑∘.⌈⍨⍳⍵}

Try it online!

Any tips on turning this into a train are welocome.

jslip

Posted 2018-07-19T13:37:37.113

Reputation: 721

My train is longer by 2

– FrownyFrog – 2018-07-19T18:06:16.067

13 bytes – FrownyFrog – 2018-07-19T18:14:08.953

3

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!

Arnauld

Posted 2018-07-19T13:37:37.113

Reputation: 111 334

3

APL (Dyalog), 12 bytes

o×⎕≥o←∘.⌈⍨⍳⎕

Try it online!

Uriel

Posted 2018-07-19T13:37:37.113

Reputation: 11 708

Would something like o×⎕≥o←∘.⌈⍨⍳ be allowed, or would you have to assign it to a function in order for that to count? – Zacharý – 2018-07-21T16:05:14.467

@Zacharý my guess is that one would need to be put it inside a tradfn with an argument or a dfns – Uriel – 2018-07-21T19:17:50.893

3

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ⱮⱮ⁹»þ¤

Jonathan Allan

Posted 2018-07-19T13:37:37.113

Reputation: 67 804

2

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

wastl

Posted 2018-07-19T13:37:37.113

Reputation: 3 089

2

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

Kevin Cruijssen

Posted 2018-07-19T13:37:37.113

Reputation: 67 575

2

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

Or Try it online!

Night2

Posted 2018-07-19T13:37:37.113

Reputation: 5 484

2

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]

I/O

Taylor Scott

Posted 2018-07-19T13:37:37.113

Reputation: 6 709

2

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

aaaaa says reinstate Monica

Posted 2018-07-19T13:37:37.113

Reputation: 381

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?

– ბიმო – 2018-07-20T00:48:10.377

@OMᗺ Octave you can't name variables inside anonymous functions in matlab. Also, max() have to take same-shape arguments – aaaaa says reinstate Monica – 2018-07-20T00:56:09.410

1An anonymous user suggested function o=f(c,s);o=zeros(c);for j=s:-1:1;o(1:s,1:s)=j;end. – Jonathan Frech – 2018-07-20T12:11:46.763

@JonathanFrech oh my so much simpler :-( just need to be o(1:j,1:j)=j – aaaaa says reinstate Monica – 2018-07-20T19:18:28.273

2

J, 18 bytes

,~@[{.[:>./~1+i.@]

Much longer than both APL solutions.

Try it online!

Galen Ivanov

Posted 2018-07-19T13:37:37.113

Reputation: 13 815

1

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.

Charlie

Posted 2018-07-19T13:37:37.113

Reputation: 11 448

1

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.

Chas Brown

Posted 2018-07-19T13:37:37.113

Reputation: 8 959

1

Charcoal, 19 bytes

Eθ⪫IEEθ⌈⟦ιλ⟧∧‹λη⊕λ 

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`
E                   Map over implicit range
      θ             Input `C`
     E              Map over implicit range
          λ         Inner index
         ι          Outer index
       ⌈⟦  ⟧        Maximium
    E               Map over results
              λ     Current value
               η    Input `S`
             ‹      Less than
                 λ  Current value
                ⊕   Incremented
            ∧       Logical AND
   I                Cast to string
  ⪫                 Join with spaces
                    Implicitly print on separate lines

Neil

Posted 2018-07-19T13:37:37.113

Reputation: 95 035

1

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.

Οurous

Posted 2018-07-19T13:37:37.113

Reputation: 7 916

1

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.

nwellnhof

Posted 2018-07-19T13:37:37.113

Reputation: 10 037

0

Mathematica 44 bytes

Table[If[i <= s && j <= s, Max[i, j], 0], {i, c}, {j, c}]

David G. Stork

Posted 2018-07-19T13:37:37.113

Reputation: 213

Are you certain the whitespace is necessary? I can't test Mathematica but I don't think it is. – Post Rock Garf Hunter – 2018-07-31T13:39:09.157