Compute the Optimal Square Matrix

13

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

user74686

Posted 2018-01-03T10:47:31.250

Reputation:

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 – 2018-08-01T10:10:11.877

Answers

7

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.

Dennis

Posted 2018-01-03T10:47:31.250

Reputation: 196 637

Oh my... Why in the world does » behave like that?! – None – 2018-01-03T13:47:15.773

5Pretty standard for an array manipulation language. Octave's max does the same. – Dennis – 2018-01-03T14:09:27.660

5

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

ბიმო

Posted 2018-01-03T10:47:31.250

Reputation: 15 345

5

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.

totallyhuman

Posted 2018-01-03T10:47:31.250

Reputation: 15 378

2I find it funny that the best I could golf in Clean is identical to your ungolfed Haskell. – Οurous – 2018-01-04T02:03:25.447

4

Octave, 13 bytes

@(A)max(A,A')

Try it online!

Steadybox

Posted 2018-01-03T10:47:31.250

Reputation: 15 798

4

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.

Steadybox

Posted 2018-01-03T10:47:31.250

Reputation: 15 798

3Also 6 bytes: _t!Xl_ and tt!&Xl. – Sanchises – 2018-01-03T15:32:20.030

3

APL (Dyalog Unicode), 3 bytes

Anonymous tacit prefix function.

⊢⌈⍉

Try it online!

 argument

 ceiling'd with

 transposed argument

Adám

Posted 2018-01-03T10:47:31.250

Reputation: 37 779

2

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

Arnauld

Posted 2018-01-03T10:47:31.250

Reputation: 111 334

2

Japt, 12 10 8 bytes

Look, Ma, no transposing or zipping!

£XËwUgEY

Try it

Shaggy

Posted 2018-01-03T10:47:31.250

Reputation: 24 623

2

J, 4 bytes

Tacit prefix function.

>.|:

Try it online!

>. ceiling [of the argument] with

|: the transposed argument

Adám

Posted 2018-01-03T10:47:31.250

Reputation: 37 779

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 – 2018-01-03T18:12:57.003

<. is supposed to be >. – FrownyFrog – 2018-01-04T03:11:31.807

@FrownyFrog Indeed. – Adám – 2018-01-04T10:31:45.820

@EriktheOutgolfer No, I don't. – Adám – 2018-01-04T10:32:03.833

1

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.

Giuseppe

Posted 2018-01-03T10:47:31.250

Reputation: 21 077

1

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

Luis Mendo

Posted 2018-01-03T10:47:31.250

Reputation: 87 464

1

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.

Jonathan Frech

Posted 2018-01-03T10:47:31.250

Reputation: 6 681

1

Clean, 58 bytes

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

I don't think this needs an explanation.

Try it online!

Οurous

Posted 2018-01-03T10:47:31.250

Reputation: 7 916

1

Julia 0.6, 13 bytes

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

a->max.(a,a')

Try it online!

gggg

Posted 2018-01-03T10:47:31.250

Reputation: 1 715

0

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.

Mr. Xcoder

Posted 2018-01-03T10:47:31.250

Reputation: 39 774

45 bytes. – totallyhuman – 2018-01-03T12:16:30.647

0

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

Emigna

Posted 2018-01-03T10:47:31.250

Reputation: 50 798

0

Jelly, 7 bytes

żZZṀ€$€

Try it online!

Mr. Xcoder

Posted 2018-01-03T10:47:31.250

Reputation: 39 774

0

Mathematica, 30 bytes

-8 bytes thanks to Jonathan Frech.

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

Try it online!

totallyhuman

Posted 2018-01-03T10:47:31.250

Reputation: 15 378

140 bytes; using Mathematica's transpose operator. – Jonathan Frech – 2018-01-04T03:21:01.033

0

Pari/GP, 21 bytes

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

Try it online!

alephalpha

Posted 2018-01-03T10:47:31.250

Reputation: 23 988

0

Wolfram Language (Mathematica), 23 bytes

A port of my Pari/GP answer.

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

is \[Transpose].

Try it online!

alephalpha

Posted 2018-01-03T10:47:31.250

Reputation: 23 988