Skip to content
Prev 19774 / 398500 Next

deleting invariant rows and cols in a matrix

Turns out my original idea didn't work, so I also figured out how
to do it recursively so that you get a matrix with no invariant
rows and columns, even if the invariant ones arise after deleting
other ones.  The function is mcyc, below.  Probably more
complicated than needed, but it works.

---------

mtest <- function(m1)
{
rowzeros <- apply(m1,1,sd)==0
colzeros <- apply(m1,2,sd)==0
m2 <- m1[rowzeros==F,colzeros==F]
d <- sum(dim(m1)-dim(m2))
return(m2,d)
}

mcyc <- function(m2)
{
m3 <- mtest(m2)
while(m3$d!=0) {m2 <- m3$m2;m3 <- mtest(m2)}
return(m3$m2)
}

-------
For example:
[1] 4 3
Message-ID: <20020512161613.A19882@cattell.psych.upenn.edu>
In-Reply-To: <20020511211739.A28457@cattell.psych.upenn.edu>; from baron@cattell.psych.upenn.edu on Sat, May 11, 2002 at 09:17:39PM -0400