hi all, i'm trying to get elements in a matrix into a vector. i need a "streamlined" way to do it as the way i'm doing it is not very serviceable. an example is a 3x3 matrix like 0 0 3 2 0 0 0 4 0 to a vector like 3 2 4 thanks...mj [[alternative HTML version deleted]]
"Mike Jones" <MikeJones at westat.com> writes:> hi all, > > i'm trying to get elements in a matrix into a vector. i need a > "streamlined" way to do it as the way i'm doing it is not very > serviceable. an example is a 3x3 matrix like > > 0 0 3 > 2 0 0 > 0 4 0 > > to a vector like > > 3 2 4You need to explain the rules of the game better. Do you mean like> M <- matrix(c(0,0,3,2,0,0,0,4,0),3,byrow=T) > rowSums(M)[1] 3 2 4 or> x <- c(t(M)) ; x[x!=0][1] 3 2 4 or ...? -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
If you are really looking for a way to extract the non-zero elements you
can use something like the following:
> library(SparseM)
> A
[,1] [,2] [,3]
[1,] 0 0 3
[2,] 2 0 0
[3,] 0 4 0
> as.matrix.csr(A)@ra
[1] 3 2 4
there is a tolerance parameter in the coercion to sparse representation
to decide what is really "zero" -- by default this is eps = .Machine
$double.eps.
url: www.econ.uiuc.edu/~roger Roger Koenker
email rkoenker at uiuc.edu Department of Economics
vox: 217-333-4558 University of Illinois
fax: 217-244-6678 Champaign, IL 61820
On Nov 9, 2005, at 10:14 AM, Mike Jones wrote:
> hi all,
>
> i'm trying to get elements in a matrix into a vector. i need a
> "streamlined" way to do it as the way i'm doing it is not
very
> serviceable. an example is a 3x3 matrix like
>
> 0 0 3
> 2 0 0
> 0 4 0
>
> to a vector like
>
> 3 2 4
>
> thanks...mj
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-
> guide.html
A matrix is a vector with a dim() attribute. Values are stored in column major order (first column on top of second column on top of ...). Thus, if mymatrix is your matrix, mymatrix[as.logical(mymatrix)] gives c(2,4,3), because as.logical(0) = FALSE, as.logical(nonzero) = TRUE. If you are really doing serious work with sparse matrices , you should look at the sparseM package. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mike Jones > Sent: Wednesday, November 09, 2005 8:14 AM > To: r-help at stat.math.ethz.ch > Subject: [R] elements in a matrix to a vector > > hi all, > > i'm trying to get elements in a matrix into a vector. i need a > "streamlined" way to do it as the way i'm doing it is not very > serviceable. an example is a 3x3 matrix like > > 0 0 3 > 2 0 0 > 0 4 0 > > to a vector like > > 3 2 4 > > thanks...mj > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Mike,
It's not clear whaty way you are doing it now, but this works
> x <- matrix(c(0,2,0,0,0,4,3,0,0),nrow=3)
> x
[,1] [,2] [,3]
[1,] 0 0 3
[2,] 2 0 0
[3,] 0 4 0
> y <- as.vector(t(x))
> z <- y[y!=0]
> z
[1] 3 2 4
Dave
Mike Jones wrote:> hi all,
>
> i'm trying to get elements in a matrix into a vector. i need a
> "streamlined" way to do it as the way i'm doing it is not
very
> serviceable. an example is a 3x3 matrix like
>
> 0 0 3
> 2 0 0
> 0 4 0
>
> to a vector like
>
> 3 2 4
>
> thanks...mj
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts office 406-994-4548
Professor and Head FAX 406-994-3190
Department of Ecology email droberts at montana.edu
Montana State University
Bozeman, MT 59717-3460
my apologies to everyone taking the time to answer this question. i
didn't explain myself very well.
i have the indices of the elements of the matrix and the matrix won't
necessarily have zeros everywhere else. is there a way to use the
indices to strip the elements i need?
thanks again...mj
-----Original Message-----
From: Dave Roberts [mailto:droberts at montana.edu]
Sent: Wednesday, November 09, 2005 12:28 PM
To: Mike Jones
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] elements in a matrix to a vector
Mike,
It's not clear whaty way you are doing it now, but this works
> x <- matrix(c(0,2,0,0,0,4,3,0,0),nrow=3)
> x
[,1] [,2] [,3]
[1,] 0 0 3
[2,] 2 0 0
[3,] 0 4 0
> y <- as.vector(t(x))
> z <- y[y!=0]
> z
[1] 3 2 4
Dave
Mike Jones wrote:> hi all,
>
> i'm trying to get elements in a matrix into a vector. i need a
> "streamlined" way to do it as the way i'm doing it is not
very
> serviceable. an example is a 3x3 matrix like
>
> 0 0 3
> 2 0 0
> 0 4 0
>
> to a vector like
>
> 3 2 4
>
> thanks...mj
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts office 406-994-4548
Professor and Head FAX 406-994-3190
Department of Ecology email droberts at montana.edu
Montana State University
Bozeman, MT 59717-3460
> my apologies to everyone taking the time to answer this question. i > didn't explain myself very well. > > i have the indices of the elements of the matrix and the matrix won't > necessarily have zeros everywhere else. is there a way to use the > indices to strip the elements i need? >RTFM ?"[".> x <- matrix(c(0, 2, 0, 0, 0, 4, 3, 0, 0), nrow=3) > x[,1] [,2] [,3] [1,] 0 0 3 [2,] 2 0 0 [3,] 0 4 0> ?"[" > xx <- matrix(c(1, 2, 3, 3, 1, 2), ncol=2) > xx[,1] [,2] [1,] 1 3 [2,] 2 1 [3,] 3 2> x[xx][1] 3 2 4>HTH, Ray