13
\$\begingroup\$

The optimal matrix (for the rather narrow scope of this challenge) is obtained by "zipping" the elements from the corresponding rows and columns of a square matrix and getting the maximum of each pair.

For instance, given the following matrix:

4 5 6
1 7 2
7 3 0

You can combine it with its transpose to get: [[[4,5,6],[4,1,7]],[[1,7,2],[5,7,3]],[[7,3,0],[6,2,0]]]. If you zip each pair of lists, you obtain the following: [[(4,4),(5,1),(6,7)],[(1,5),(7,7),(2,3)],[(7,6),(3,2),(0,0)]]. The last step is to get the maximum of each pair to get the optimal matrix:

4 5 7
5 7 3
7 3 0

Your task is to output the optimal matrix of a square matrix given as input. The matrix will only contain integers. I/O can be done in any reasonable format. The shortest code in bytes (either in UTF-8 or in the language's custom encoding) wins!

Tests

[[172,29],[29,0]] -> [[172,29],[29,0]]
[[4,5,6],[1,7,2],[7,3,0]] -> [[4,5,7],[5,7,3],[7,3,0]]
[[1,2,3],[1,2,3],[1,2,3]] -> [[1,2,3],[2,2,3],[3,3,3]]
[[4,5,-6],[0,8,-12],[-2,2,4]] -> [[4,5,-2],[5,8,2],[-2,2,4]]
\$\endgroup\$
1
  • \$\begingroup\$ Can we output a flat version of the matrix? e.g. [1,2,3,4] instead of [[1,2],[3,4]]? Would save ~33% \$\endgroup\$
    – wastl
    Aug 1, 2018 at 10:10

20 Answers 20

7
\$\begingroup\$

Jelly, 2 bytes

»Z

Try it online!

How it works

»Z  Main link. Argument: M (integer matrix)

 Z  Zip the rows of M, transposing rows and columns.
»   Take the maxima of all corresponding integers.
\$\endgroup\$
2
  • \$\begingroup\$ Oh my... Why in the world does » behave like that?! \$\endgroup\$
    – user74686
    Jan 3, 2018 at 13:47
  • 5
    \$\begingroup\$ Pretty standard for an array manipulation language. Octave's max does the same. \$\endgroup\$
    – Dennis
    Jan 3, 2018 at 14:09
5
\$\begingroup\$

Haskell, 40 bytes

z(z max)<*>foldr(z(:))e
e=[]:e
z=zipWith

Try it online!

I would ungolf this as:

import Data.List
f m = zipWith (zipWith max) m (transpose m)

...which is so much more elegant.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I find it funny that the best I could golf in Clean is identical to your ungolfed Haskell. \$\endgroup\$
    – Οurous
    Jan 4, 2018 at 2:03
5
\$\begingroup\$

Husk, 5 4 bytes

Whoop, never got to use before (or ):

S‡▲T

Try it online!

Explanation

S  T -- apply the function to itself and itself transposed
 ‡▲  -- bi-vectorized maximum
\$\endgroup\$
4
\$\begingroup\$

Octave, 13 bytes

@(A)max(A,A')

Try it online!

\$\endgroup\$
4
\$\begingroup\$

MATL, 6 bytes

t!2$X>

Try it online!

Explanation:

t        % Duplicate the input.
!        % Transpose the duplicate.
2$X>     % Elementwise maximum of the two matrices.
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Also 6 bytes: _t!Xl_ and tt!&Xl. \$\endgroup\$
    – Sanchises
    Jan 3, 2018 at 15:32
3
\$\begingroup\$

APL (Dyalog Unicode), 3 bytes

Anonymous tacit prefix function.

⊢⌈⍉

Try it online!

 argument

 ceiling'd with

 transposed argument

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 48 bytes

m=>m.map((r,y)=>r.map((v,x)=>v>(k=m[x][y])?v:k))

Test cases

let f =

m=>m.map((r,y)=>r.map((v,x)=>v>(k=m[x][y])?v:k))

console.log(JSON.stringify(f([[172,29],[29,0]]            ))) // -> [[172,29],[29,0]]
console.log(JSON.stringify(f([[4,5,6],[1,7,2],[7,3,0]]    ))) // -> [[4,5,7],[5,7,3],[7,3,0]]
console.log(JSON.stringify(f([[1,2,3],[1,2,3],[1,2,3]]    ))) // -> [[1,2,3],[2,2,3],[3,3,3]]
console.log(JSON.stringify(f([[4,5,-6],[0,8,-12],[-2,2,4]]))) // -> [[4,5,-2],[5,8,2],[-2,2,4]]

\$\endgroup\$
2
\$\begingroup\$

J, 4 bytes

Tacit prefix function.

>.|:

Try it online!

>. ceiling [of the argument] with

|: the transposed argument

\$\endgroup\$
4
  • \$\begingroup\$ Um, I don't think you need to include f=:. :P at first I thought you reduced the bytecount by 3 bytes... \$\endgroup\$ Jan 3, 2018 at 18:12
  • \$\begingroup\$ <. is supposed to be >. \$\endgroup\$
    – FrownyFrog
    Jan 4, 2018 at 3:11
  • \$\begingroup\$ @FrownyFrog Indeed. \$\endgroup\$
    – Adám
    Jan 4, 2018 at 10:31
  • \$\begingroup\$ @EriktheOutgolfer No, I don't. \$\endgroup\$
    – Adám
    Jan 4, 2018 at 10:32
2
\$\begingroup\$

Japt, 12 10 8 bytes

Look, Ma, no transposing or zipping!

£XËwUgEY

Try it

\$\endgroup\$
1
\$\begingroup\$

CJam, 8 bytes

{_z..e>}

Anonymous block (function) that takes the input from the stack and replaces it by the output.

Try it online! Or verify all test cases.

Explanation

{      }    e# Define block
 _          e# Duplicate
  z         e# Zip
   .        e# Apply next operator to the two arrays, item by item
            e# (that is, to rows of the two matrices)
    .       e# Apply next operator to the two arrays, item by item
            e# (that is, to numbers of the two rows)
     e>     e# Maximum of two numbers
\$\endgroup\$
1
\$\begingroup\$

R, 23 bytes

function(A)pmax(A,t(A))

Try it online!

This is equivalent to most other answers. However, R has two distinct max functions for the two common scenarios:

max and min return the maximum or minimum of all the values present in their arguments, as integer if all are logical or integer, as double if all are numeric, and character otherwise.

pmax and pmin take one or more vectors (or matrices) as arguments and return a single vector giving the ‘parallel’ maxima (or minima) of the vectors. The first element of the result is the maximum (minimum) of the first elements of all the arguments, the second element of the result is the maximum (minimum) of the second elements of all the arguments and so on. Shorter inputs (of non-zero length) are recycled if necessary.

\$\endgroup\$
1
\$\begingroup\$

Clean, 58 bytes

import StdEnv,StdLib
@l=zipWith(zipWith max)(transpose l)l

I don't think this needs an explanation.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 79 77 bytes

  • Saved two bytes thanks to Steadybox; only taking in one matrix dimension parameter as all matrices in this challenge are square.
j,i;f(A,n)int*A;{for(j=0;j<n*n;j++)printf("%d,",A[A[j]>A[i=j/n+j%n*n]?j:i]);}

Try it online!

Takes a flat integer array A and the matrix dimension n (as the matrix has to be square) as input. Outputs a flat integer array string representation to stdout.

\$\endgroup\$
0
1
\$\begingroup\$

Wolfram Language (Mathematica), 23 bytes

A port of my Pari/GP answer.

(#+#+Abs[#-#])/2&

is \[Transpose].

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 13 bytes

max. applies the function max elementwise to it's arugments.

a->max.(a,a')

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

05AB1E, 7 bytes

ø‚øεøεà

Try it online!

Explanation

ø         # transpose input matrix
 ‚        # pair with original matrix
  ø       # zip together
   ε      # apply on each sublist ([[row],[transposed row]])
    ø     # zip
     ε    # apply on each sublist (pair of elements)
      à   # extract greatest element
\$\endgroup\$
0
\$\begingroup\$

Jelly, 7 bytes

żZZṀ€$€

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 45 bytes

lambda k:[map(max,*c)for c in zip(k,zip(*k))]

Try it online!

Thanks to totallyhuman for a few bytes saved.

\$\endgroup\$
1
0
\$\begingroup\$

Pari/GP, 21 bytes

m->(m+m~+abs(m-m~))/2

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 30 bytes

-8 bytes thanks to Jonathan Frech.

Map@Max/@t/@t@{#,t@#}&
t=#&

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 40 bytes; using Mathematica's transpose operator. \$\endgroup\$ Jan 4, 2018 at 3:21
  • \$\begingroup\$ How would you implement this in an Mathematica notebook using, say, the 2nd test case ({{4,5,6},{1,7,2},{7,3,0}})? \$\endgroup\$
    – theorist
    Feb 10, 2022 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.