| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | Wolfram Language Mathematica | 241105T210702Z | att |
| 003 | Uiua | 241106T125657Z | nyxbird |
| 077 | Scala 3 | 241106T073033Z | 138 Aspe |
| 006 | Japt m | 180103T114217Z | Shaggy |
| 013 | Julia 0.6 | 180104T162854Z | gggg |
| 030 | Mathematica | 180104T024226Z | totallyh |
| 023 | Wolfram Language Mathematica | 180104T145151Z | alephalp |
| 004 | J | 180103T151105Z | Adá |
| 021 | Pari/GP | 180104T081842Z | alephalp |
| 077 | C gcc | 180104T015059Z | Jonathan |
| 058 | Clean | 180104T020200Z | Οurous |
| 023 | R | 180103T123653Z | Giuseppe |
| 003 | APL Dyalog Unicode | 180103T145453Z | Adá |
| 002 | Jelly | 180103T133426Z | Dennis |
| 008 | CJam | 180103T132637Z | Luis Men |
| 004 | Husk | 180103T112000Z | ბიმო |
| 045 | Python 2 | 180103T105326Z | Mr. Xcod |
| 040 | Haskell | 180103T115721Z | totallyh |
| 006 | MATL | 180103T115448Z | Steadybo |
| 007 | Jelly | 180103T110154Z | Mr. Xcod |
| 007 | 05AB1E | 180103T105537Z | Emigna |
| 048 | JavaScript ES6 | 180103T105319Z | Arnauld |
| 013 | Octave | 180103T105212Z | Steadybo |
Wolfram Language (Mathematica), 15 bytes
#+Ramp[#-#]&
Wolfram Language (Mathematica), 19 bytes
#~In~#/.In->Max&
In is the most convenient Listable head.
#~In~# pair elements of matrix and its transpose
/.In->Max max each pair
Scala 3, 77 bytes
Golfed version. Attempt This Online!
k=>k.zip(k.transpose).map{case(x,y)=>(x zip y).map{case(a,b)=>Math.max(a,b)}}
Ungolfed version. Attempt This Online!
object Main {
def f(k: List[List[Int]]): List[List[Int]] = {
k.zip(k.transpose).map {
case (row1, row2) => (row1 zip row2).map { case (a, b) => Math.max(a, b) }
}
}
def main(args: Array[String]): Unit = {
val tests = List(
List(List(4, 5, 6), List(1, 7, 2), List(7, 3, 0)),
List(List(1, 2, 3), List(1, 2, 3), List(1, 2, 3)),
List(List(4, 5, -6), List(0, 8, -12), List(-2, 2, 4)),
List(List(172, 29), List(29, 0))
)
val solutions = List(
List(List(4, 5, 7), List(5, 7, 3), List(7, 3, 0)),
List(List(1, 2, 3), List(2, 2, 3), List(3, 3, 3)),
List(List(4, 5, -2), List(5, 8, 2), List(-2, 2, 4)),
List(List(172, 29), List(29, 0))
)
// First check: print results with explanations
for ((test, solution) <- tests zip solutions) {
val result = f(test)
if (result == solution) {
println(f"The result is correct: ${result.toString.padTo(36, ' ')}.")
} else {
println(f"The result is wrong: ${result.toString.padTo(36, ' ')}.")
}
}
// Second check: assert correctness
for ((test, solution) <- tests zip solutions) {
assert(f(test) == solution)
}
println("\nAll checks have passed!")
}
}
Wolfram Language (Mathematica), 23 bytes
A port of my Pari/GP answer.
(#+#+Abs[#-#])/2&
is \[Transpose].
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]);}
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.
R, 23 bytes
function(A)pmax(A,t(A))
This is equivalent to most other answers. However, R has two distinct max functions for the two common scenarios:
maxandminreturn 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.
pmaxandpmintake 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.
⊢⌈⍉
⊢ argument
⌈ ceiling'd with
⍉ transposed argument
Jelly, 2 bytes
»Z
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.
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
Husk, 5 4 bytes
Whoop, never got to use ‡ before (or †):
S‡▲T
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))]
Thanks to totallyhuman for a few bytes saved.
Haskell, 40 bytes
z(z max)<*>foldr(z(:))e
e=[]:e
z=zipWith
I would ungolf this as:
import Data.List
f m = zipWith (zipWith max) m (transpose m)
...which is so much more elegant.
MATL, 6 bytes
t!2$X>
Explanation:
t % Duplicate the input.
! % Transpose the duplicate.
2$X> % Elementwise maximum of the two matrices.
05AB1E, 7 bytes
ø‚øεøεà
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]]