Skip to content
Prev 59508 / 63430 Next

surprised matrix (1:256, 8, 8) doesn't cause error/warning

Hi Martin,

It kind of does make sense to issue the warning when **recycling** (and 
this is consistent with what happens with recycling in general):

   > matrix(1:4, 6, 6)
        [,1] [,2] [,3] [,4] [,5] [,6]
   [1,]    1    3    1    3    1    3
   [2,]    2    4    2    4    2    4
   [3,]    3    1    3    1    3    1
   [4,]    4    2    4    2    4    2
   [5,]    1    3    1    3    1    3
   [6,]    2    4    2    4    2    4

   > matrix(1:4, 5, 6)
        [,1] [,2] [,3] [,4] [,5] [,6]
   [1,]    1    2    3    4    1    2
   [2,]    2    3    4    1    2    3
   [3,]    3    4    1    2    3    4
   [4,]    4    1    2    3    4    1
   [5,]    1    2    3    4    1    2
   Warning message:
   In matrix(1:4, 5, 6) :
     data length [4] is not a sub-multiple or multiple of the number of 
rows [5]

(Note that the warning is misleading. matrix() is happy to take data 
with a length that is not a sub-multiple of the number of rows or cols 
as long as it's a sub-multiple of the length of the matrix.)

However I'm not sure that **truncating** the data is desirable behavior:

   > matrix(1:6, 1, 3)
        [,1] [,2] [,3]
   [1,]    1    2    3

   > matrix(1:6, 1, 5)
        [,1] [,2] [,3] [,4] [,5]
   [1,]    1    2    3    4    5
   Warning message:
   In matrix(1:6, 1, 5) :
   data length [6] is not a sub-multiple or multiple of the number of 
columns [5]

Maybe you get a warning sometimes, if you are lucky, but still.

Finally note that you never get any warning with array():

   > array(1:4, c(5, 6))
        [,1] [,2] [,3] [,4] [,5] [,6]
   [1,]    1    2    3    4    1    2
   [2,]    2    3    4    1    2    3
   [3,]    3    4    1    2    3    4
   [4,]    4    1    2    3    4    1
   [5,]    1    2    3    4    1    2

   > array(1:6, c(1, 5))
        [,1] [,2] [,3] [,4] [,5]
   [1,]    1    2    3    4    5

Cheers,
H.
On 2/1/21 1:08 AM, Martin Maechler wrote: