15
\$\begingroup\$

You're given a n-by-m matrix of integers, where n,m > 3. Your task is to find the 3-by-3 sub-matrix that has the lowest mean, and output this value.

Rules and clarifications:

  • The integers will be non-negative
  • Optional input and output format
  • The output must be accurate up to at least 2 decimal poins (if it's non-integer)
  • The submatrices can be made up of arbitrary columns and rows

Test cases:

1   0   4   0   1   0
1   0   4   0   1   0
4   3   4   3   4   3
1   0   4   0   1   0

Minimum mean: 0   (We have chosen columns 2,4,6 and rows 1,2,4 (1-indexed)
-----------------------------
4    8    9    7
5   10    1    5
8    5    2    4
8    3    5   10
6    6    3    4

Minimum mean: 4.2222
-----------------------------
1   0   0   0   0
0   2   0   0   0
0   0   3   0   0
0   0   0   4   0
0   0   0   0   5

Minimum mean: 0.11111
-----------------------------
371   565   361   625   879   504   113   104
943   544   157   799   726   832   228   405
743   114   171   506   943   181   823   454
503   410   333   735   554   227   423   662
629   439   191   707    52   751   506   924

Minimum mean: 309.56
\$\endgroup\$
  • \$\begingroup\$ What makes this different from the first version of this challenge? \$\endgroup\$ – Kritixi Lithos Feb 6 '17 at 14:17
  • 2
    \$\begingroup\$ @KritixiLithos It uses the more general definition of "submatrix" where a submatrix is any matrix you can obtain from deleting any number of rows and columns from the original (so the remaining rows/columns don't have to be adjacent). \$\endgroup\$ – Martin Ender Feb 6 '17 at 14:20
9
\$\begingroup\$

Mathematica, 77 50 bytes

±x_:=x~Subsets~{3}
Min[Mean/@Mean/@±#&/@±#]&

is Mathematica's transposition operator (and is rendered as a superscript T in Mathematica).

This answer first defines a helper operator ± which returns all 3-element subsets of a list, and then evaluates to an unnamed function which uses this operator to solve the problem.

This is done by first computing all 3-element subsets of the matrix's rows. Then for each such subset, we transpose it and compute its 3-element subset of rows. This gives us all possible 3x3 submatrices (although they are transposed). We then compute the mean on all of them and find the overall minimum.

\$\endgroup\$
7
\$\begingroup\$

Jelly, 15 12 bytes

œc3S€Zµ⁺FṂ÷9

Try it online!

How it works

œc3S€Zµ⁺FṂ÷9  Main link. Argument: M (matrix)

œc3           Yield all combinations of 3 rows.
   S€         Map column-wise sum over the combinations.
     Z        Zip, transposing rows and columns.
      µ       Combine all links to the left into a chain.
       ⁺      Duplicate the chain, executing it twice.
        F     Flatten.
         Ṃ    Take the minimum.
          ÷9  Divide it by 9.
\$\endgroup\$
  • \$\begingroup\$ œc3S€µ⁺€FṂ÷9 is what I got... EDIT - hah and just like that you do the same :D \$\endgroup\$ – Jonathan Allan Feb 6 '17 at 17:00
  • \$\begingroup\$ Ninja'd by 17 seconds. :P Thanks anyway. :) \$\endgroup\$ – Dennis Feb 6 '17 at 17:08
  • \$\begingroup\$ I can't help but think there is a way to get rid of the 9 by dividing by 3 inside the repeated chain, but is it possible to get 3 as the right argument such that it's possible in 11? \$\endgroup\$ – Jonathan Allan Feb 6 '17 at 17:10
  • \$\begingroup\$ Not in one byte, and that's what it would take to save one. You can't place 3 outside the chain (both because it's monadic and you'd have to group it to use ), and inside the chain you either have to specify 3 explicitly or group it with ÷. \$\endgroup\$ – Dennis Feb 6 '17 at 17:16
4
\$\begingroup\$

05AB1E, 21 16 bytes

2Fvyæ3ùO})ø}˜9/W

Try it online!

Explanation

  • For each row, get the sum of each ordered subset of size 3
  • Transpose the resulting matrix
  • For each row, get the sum of each ordered subset of size 3
  • Flatten the resulting matrix
  • Divide by 9
  • Get the minimum
\$\endgroup\$
1
\$\begingroup\$

Haskell, 90 bytes

import Data.List
t r=[a+b+c|[a,b,c]<-subsequences r]
s=(/9).minimum.(t=<<).transpose.map t

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ concatMap t can be shortened to (>>=t) \$\endgroup\$ – Laikoni Feb 9 '17 at 15:28
0
\$\begingroup\$

Bean, 198 bytes

Hexdump:

00000000 bc 81 bd a0 65 40 a0 5d dd a0 68 50 80 a0 77 20  ¼.½ e@ ]Ý hP. w 
00000010 80 01 dd a0 66 25 3b 52 cc cb c0 50 84 a0 5d 20  ..Ý f%;RÌËÀP. ] 
00000020 66 87 4c cc a0 68 8b 20 66 8c 25 3b cd d0 84 a0  f.LÌ h. f.%;ÍÐ. 
00000030 5d 20 66 80 4e a0 66 81 4c d3 a0 65 a0 5d a0 68  ] f.N f.LÓ e ] h
00000040 4c a0 66 8c 25 3a 8b 25 3a 50 84 a0 5d 20 66 bd  L f.%:.%:P. ] f½
00000050 a0 6e 43 a5 39 a5 3a a5 3b 00 bd a0 5f 43 cf 20   nC¥9¥:¥;.½ _CÏ 
00000060 6e 00 3d a0 69 20 12 b6 a7 36 a7 26 4d a0 69 80  n.= i .¶§6§&M i.
00000070 53 d0 80 a0 1f 20 80 45 a0 69 53 d0 80 a0 6e 20  SÐ. . .E iSÐ. n 
00000080 80 8b 40 a0 6f a0 75 4c a0 6f 8b 53 d0 80 a0 5f  ..@ o uL o.SÐ. _
00000090 20 80 8b 40 a0 6f a0 74 4c a0 6f 8b 50 84 d0 84   ..@ o tL o.P.Ð.
000000a0 a0 77 20 75 20 74 4c d3 a0 65 a0 5f 50 80 a0 43   w u tLÓ e _P. C
000000b0 20 80 01 81 25 3b 4c d3 a0 65 20 6e 81 25 3b 26   ...%;LÓ e n.%;&
000000c0 4c a0 69 8e 25 42                                L i.%B
000000c6

Equivalent JavaScript:

// indices array increment function
var i=(a,l=$.length,j=2)=>++a[j]>=l+j-2?a[j]=j&&i(a,l,j-1)+1:a[j],
// row indices
    r=[0,1,2],
// column indices
    c=[...r],
// minimum sum
    m=Infinity;
do{
  do{
// calculate sum of current row/column indices and keep minimum
    m=Math.min(m,
      (r.reduce((s,y)=>s+c.reduce((s,x)=>s+$[y][x])))
    )
// until column indices loop
  }while(i(c,A.length)!=2)
// until row indices loop
}while(i(r)!=2)
// output mean
m/9

Try demo here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.