Re: [R] fusion of two matrices (numerical and logical)

2020-09-19 Thread Richard O'Keefe
(1) Using 'C == TRUE' (when you know C is logical)
is equivalent to just plain C, only obscure.
Similarly, 'C == FALSE' is more confusing than !C.

(2) Consider B[C].  The rows of C have 2, 1, 1, 2, 1 TRUE.
entries, so the result here *cannot* be a rectangular array.
And whatever it is, it contains only the elements where C
is true.

(3) You probably already knew that 'ifelse' is vectorised.
What you may not have realised is that it preserves
array dimensions as well.

> A <- cbind(c(1,2), c(3,4))
> B <- cbind(c(5,6), c(7,8))
> C <- cbind(c(FALSE,TRUE), c(TRUE,FALSE))
> ifelse(C, A, B)
 [,1] [,2]
[1,]53
[2,]28
> ifelse(C, A, 0)
 [,1] [,2]
[1,]03
[2,]20

Isn't it nice when the obvious code just works?


On Sun, 6 Sep 2020 at 06:18, Vivek Sutradhara  wrote:

> Hi
> I would like to get help in combining two matrices. Here is my example:
> A <- 1:20
> B <- matrix(A,nrow=5,ncol=4)
> # B is a numerical matrix
> C <- B<7
> C[4,4] <- TRUE
> # C is a logical matrix
> # if I combine A and C, I get a vector
> D1 <- A[C==TRUE]
> D1
> D2 <- A[C==FALSE]
> D2
>
> I want to get a matrix with the same dimensions as matrix A. At the
> coordinates given by the vector D1, I want to retain the values in
> matrix A. At the locations in D2, I want a zero value.
> I want to know if I can do this without using any loops.
> Thanks, Vivek
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] fusion of two matrices (numerical and logical)

2020-09-18 Thread jim holtman
Here is a way of doing it using the 'arr.ind' option in 'which'

> A <- 1:20
> B <- matrix(A,nrow=5,ncol=4)
> B
 [,1] [,2] [,3] [,4]
[1,]16   11   16
[2,]27   12   17
[3,]38   13   18
[4,]49   14   19
[5,]5   10   15   20
> # B is a numerical matrix
> C <- B<7
> C[4,4] <- TRUE
> C
 [,1]  [,2]  [,3]  [,4]
[1,] TRUE  TRUE FALSE FALSE
[2,] TRUE FALSE FALSE FALSE
[3,] TRUE FALSE FALSE FALSE
[4,] TRUE FALSE FALSE  TRUE
[5,] TRUE FALSE FALSE FALSE
>
> # initialize a 'result' with zeros
> result <- array(0, dim = dim(B))
>
> # get the indices of values to replace
> indx <- which(C, arr.ind = TRUE)
>
> result[indx] <- B[indx]
>
> result
 [,1] [,2] [,3] [,4]
[1,]1600
[2,]2000
[3,]3000
[4,]400   19
[5,]5000
>


Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Sat, Sep 5, 2020 at 11:51 AM Bert Gunter  wrote:

> A is not a matrix. I presume you meant B. If so:
>
> > B[!C] <- 0
> > B
>  [,1] [,2] [,3] [,4]
> [1,]1600
> [2,]2000
> [3,]3000
> [4,]400   19
> [5,]5000
>
> Cheers,
> Bert
>
>
>
>
>
> On Sat, Sep 5, 2020 at 11:18 AM Vivek Sutradhara 
> wrote:
>
> > Hi
> > I would like to get help in combining two matrices. Here is my example:
> > A <- 1:20
> > B <- matrix(A,nrow=5,ncol=4)
> > # B is a numerical matrix
> > C <- B<7
> > C[4,4] <- TRUE
> > # C is a logical matrix
> > # if I combine A and C, I get a vector
> > D1 <- A[C==TRUE]
> > D1
> > D2 <- A[C==FALSE]
> > D2
> >
> > I want to get a matrix with the same dimensions as matrix A. At the
> > coordinates given by the vector D1, I want to retain the values in
> > matrix A. At the locations in D2, I want a zero value.
> > I want to know if I can do this without using any loops.
> > Thanks, Vivek
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] fusion of two matrices (numerical and logical)

2020-09-05 Thread Bert Gunter
A is not a matrix. I presume you meant B. If so:

> B[!C] <- 0
> B
 [,1] [,2] [,3] [,4]
[1,]1600
[2,]2000
[3,]3000
[4,]400   19
[5,]5000

Cheers,
Bert





On Sat, Sep 5, 2020 at 11:18 AM Vivek Sutradhara 
wrote:

> Hi
> I would like to get help in combining two matrices. Here is my example:
> A <- 1:20
> B <- matrix(A,nrow=5,ncol=4)
> # B is a numerical matrix
> C <- B<7
> C[4,4] <- TRUE
> # C is a logical matrix
> # if I combine A and C, I get a vector
> D1 <- A[C==TRUE]
> D1
> D2 <- A[C==FALSE]
> D2
>
> I want to get a matrix with the same dimensions as matrix A. At the
> coordinates given by the vector D1, I want to retain the values in
> matrix A. At the locations in D2, I want a zero value.
> I want to know if I can do this without using any loops.
> Thanks, Vivek
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] fusion of two matrices (numerical and logical)

2020-09-05 Thread Vivek Sutradhara
The result that I want to get is this:
for (i in 1:5) {
  for (j in 1:4) {
B[i,j] <- ifelse(C[i,j]==FALSE,0,B[i,j])
  }
}
I would like to know if I can do this without loops.

Den lör 5 sep. 2020 kl 20:18 skrev Vivek Sutradhara :

> Hi
> I would like to get help in combining two matrices. Here is my example:
> A <- 1:20
> B <- matrix(A,nrow=5,ncol=4)
> # B is a numerical matrix
> C <- B<7
> C[4,4] <- TRUE
> # C is a logical matrix
> # if I combine A and C, I get a vector
> D1 <- A[C==TRUE]
> D1
> D2 <- A[C==FALSE]
> D2
>
> I want to get a matrix with the same dimensions as matrix A. At the
> coordinates given by the vector D1, I want to retain the values in
> matrix A. At the locations in D2, I want a zero value.
> I want to know if I can do this without using any loops.
> Thanks, Vivek
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] fusion of two matrices (numerical and logical)

2020-09-05 Thread Vivek Sutradhara
Hi
I would like to get help in combining two matrices. Here is my example:
A <- 1:20
B <- matrix(A,nrow=5,ncol=4)
# B is a numerical matrix
C <- B<7
C[4,4] <- TRUE
# C is a logical matrix
# if I combine A and C, I get a vector
D1 <- A[C==TRUE]
D1
D2 <- A[C==FALSE]
D2

I want to get a matrix with the same dimensions as matrix A. At the
coordinates given by the vector D1, I want to retain the values in
matrix A. At the locations in D2, I want a zero value.
I want to know if I can do this without using any loops.
Thanks, Vivek

[[alternative HTML version deleted]]

__
R-help@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.