Frederic Ntirenganya
2015-Mar-11 07:01 UTC
[R] Comparing each component of vector to each component of a Matrix.
Hi All, I need a help on a loop which can compare the each component of vector to each component of a Matrix. Let a vector b = {b1, b2, ...,bn} and 2x2 matrix A = Aij. I need to compare each component of the vector with each component of a matrix and print 1 if the component is less or equal to one and print 0 otherwise. bi <= Aij, print 1 and 0 otherwise. Any help is appreciated. Regards, Frederic. Frederic Ntirenganya Maseno University, African Maths Initiative, Kenya. Mobile:(+254)718492836 Email: fredo at aims.ac.za https://sites.google.com/a/aims.ac.za/fredo/ [[alternative HTML version deleted]]
Sven E. Templer
2015-Mar-11 07:26 UTC
[R] Comparing each component of vector to each component of a Matrix.
Hi, you didn't specify values in A, and you first wanted to compare bi with Aij, but then also which bi is less/equal to zero. For the first case, with A <- matrix(0:3,2) b <- seq(-1,5) and a comparison function for bi less/equal to Aij like f <- function (bi) {as.integer(bi<=A)} you can iterate along b with lapply(b, f) # contains results as matrices in a list, each with same dimensions as A sapply(b, f) # aligns results to an length(A)*length(b) matrix Best, S. On 11 March 2015 at 08:01, Frederic Ntirenganya <ntfredo at gmail.com> wrote:> Hi All, > > I need a help on a loop which can compare the each component of vector to > each component of a Matrix. > > Let a vector b = {b1, b2, ...,bn} and 2x2 matrix A = Aij. > > I need to compare each component of the vector with each component of a > matrix and print 1 if the component is less or equal to one and print 0 > otherwise. > > bi <= Aij, print 1 and 0 otherwise. > > Any help is appreciated. > > Regards, > Frederic. > > > Frederic Ntirenganya > Maseno University, > African Maths Initiative, > Kenya. > Mobile:(+254)718492836 > Email: fredo at aims.ac.za > https://sites.google.com/a/aims.ac.za/fredo/ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Rolf Turner
2015-Mar-11 09:46 UTC
[R] Comparing each component of vector to each component of a Matrix.
On 11/03/15 20:01, Frederic Ntirenganya wrote:> Hi All, > > I need a help on a loop which can compare the each component of vector to > each component of a Matrix. > > Let a vector b = {b1, b2, ...,bn} and 2x2 matrix A = Aij. > > I need to compare each component of the vector with each component of a > matrix and print 1 if the component is less or equal to one and print 0 > otherwise. > > bi <= Aij, print 1 and 0 otherwise. > > Any help is appreciated.outer(b,A,"<=")+0 cheers, Rolf Turner -- Rolf Turner Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 Home phone: +64-9-480-4619