You are here

matrix not populated as expected in R, based on nrow & ncol

> nv <- 1
> ntv <- nv*2
> nth <-3
> thLBound <- matrix(rep(c(-3,(rep(0.001,nth-1))),nv),nrow=nth,ncol=nv)

Given these defined variables, I expected that the values and lbounds for the matrix to be created below would be populated repeatedly until the desired number of rows/columns based on nrow and ncol was obtained, but it appears that the dimensions of the matrix are determined by the values and lbound arguments.

> mxMatrix( type="Full", nrow=nth, ncol=ntv, free=TRUE, values=c(-1,.5,.5), lbound=thLBound, name="Thre")
FullMatrix 'Thre'

@labels: No labels assigned.

@values
[,1]
[1,] -1.0
[2,] 0.5
[3,] 0.5

@free
[,1]
[1,] TRUE
[2,] TRUE
[3,] TRUE

@lbound
[,1]
[1,] -3.000
[2,] 0.001
[3,] 0.001

@ubound: No upper bounds assigned.

Warning messages:
1: 'nrow' is disregarded for mxMatrix constructor in mxMatrix(type = "Full", nrow = nth, ncol = ntv, free = TRUE, values = c(-1, 0.5, 0.5), lbound = thLBound, name = "Thre")
2: 'ncol' is disregarded for mxMatrix constructor in mxMatrix(type = "Full", nrow = nth, ncol = ntv, free = TRUE, values = c(-1, 0.5, 0.5), lbound = thLBound, name = "Thre")
>
> ntv
[1] 2
> mxMatrix( type="Full", nrow=nth, ncol=ntv, free=TRUE, values=c(-1,.5,.5,-1,.5,.5), lbound=cbind(thLBound,thLBound), name="Thre")
FullMatrix 'Thre'

@labels: No labels assigned.

@values
[,1] [,2]
[1,] -1.0 -1.0
[2,] 0.5 0.5
[3,] 0.5 0.5

@free
[,1] [,2]
[1,] TRUE TRUE
[2,] TRUE TRUE
[3,] TRUE TRUE

@lbound
[,1] [,2]
[1,] -3.000 -3.000
[2,] 0.001 0.001
[3,] 0.001 0.001

@ubound: No upper bounds assigned.

Warning messages:
1: 'nrow' is disregarded for mxMatrix constructor in mxMatrix(type = "Full", nrow = nth, ncol = ntv, free = TRUE, values = c(-1, 0.5, 0.5, -1, 0.5, 0.5), lbound = cbind(thLBound, thLBound), name = "Thre")
2: 'ncol' is disregarded for mxMatrix constructor in mxMatrix(type = "Full", nrow = nth, ncol = ntv, free = TRUE, values = c(-1, 0.5, 0.5, -1, 0.5, 0.5), lbound = cbind(thLBound, thLBound), name = "Thre")

Reporter: 
Created: 
Wed, 07/21/2010 - 23:30
Updated: 
Thu, 07/22/2010 - 10:54

Comments

I think the point of allowing matrices as inputs for mxMatrix parameters was so that users could work with matrices (vectors that have their own internal row and column count), and ignore/not provide the mxMatrix nrow and ncol.

So this seems to be working as expected to me: when you provide a matrix, the nrow and ncol arguments are overriden so that the matrix you provide as input can be a vliad pattern for the mxMatrix.

This works just fine if instead of
thLBound <- matrix(rep(c(-3,(rep(0.001,nth-1))),nv),nrow=nth,ncol=nv)
you say
thLBound <- rep(c(-3,(rep(0.001,nth-1))),nv)

Yeah, I think we screwed up in this case. The matrix() function is fairly flexible in R. If I try the following expressions, they all work:

matrix(thLBound, nrow = 3, ncol = 2)
matrix(thLBound, nrow = 2, ncol = 2)
matrix(thLBound, nrow = 6, ncol = 1)
matrix(thLBound, nrow = 6, ncol = 2)
matrix(thLBound, nrow = 5, ncol = 2)
matrix(thLBound, nrow = 1, ncol = 1)

I think we should change mxMatrix() to behave like matrix(). I'll even throw the appropriate warnings when the data length is not a sub-multiple or multiple of the numbers of rows/cols. Of course, there's the smallest chance that somebody has written their scripts relying on the old behavior. And the behavior is error-prone. I'm surprised that the last expression doesn't throw a warning. But we are pre-1.0 release, so this would be a good time to make the change.