# Compute the Optimal Square Matrix

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

• Can we output a flat version of the matrix? e.g. [1,2,3,4] instead of [[1,2],[3,4]]? Would save ~33% – wastl Aug 1 '18 at 10:10

# Japt, 1210 8 bytes

Look, Ma, no transposing or zipping!

£XËwUgEY


Try it

# Julia 0.6, 13 bytes

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

a->max.(a,a')


Try it online!

# Mathematica, 30 bytes

-8 bytes thanks to Jonathan Frech.

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


Try it online!

# Wolfram Language (Mathematica), 23 bytes

A port of my Pari/GP answer.

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


 is \[Transpose].

Try it online!

# J, 4 bytes

Tacit prefix function.

>.|:


Try it online!

>. ceiling [of the argument] with

|: the transposed argument

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

# Pari/GP, 21 bytes

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


Try it online!

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

# Clean, 58 bytes

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


I don't think this needs an explanation.

Try it online!

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

# APL (Dyalog Unicode), 3 bytes

Anonymous tacit prefix function.

⊢⌈⍉


Try it online!

⊢ argument

⌈ ceiling'd with

⍉ transposed argument

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

• Oh my... Why in the world does » behave like that?! – user74686 Jan 3 '18 at 13:47
• Pretty standard for an array manipulation language. Octave's max does the same. – Dennis Jan 3 '18 at 14:09

# CJam, 8 bytes

{_z..e>}


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

### 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


# 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


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

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.

• I find it funny that the best I could golf in Clean is identical to your ungolfed Haskell. – Οurous Jan 4 '18 at 2:03

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

• Also 6 bytes: _t!Xl_ and tt!&Xl. – Sanchises Jan 3 '18 at 15:32

# Jelly, 7 bytes

żZZṀ€\$€


Try it online!

# 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


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

# Octave, 13 bytes

@(A)max(A,A')


Try it online!