# fusion of two matrices (numerical and logical)

5 messages
Open this post in threaded view
|

## fusion of two matrices (numerical and logical)

 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: fusion of two matrices (numerical and logical)

 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 <[hidden email]>: > 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: fusion of two matrices (numerical and logical)

 In reply to this post by vivek A is not a matrix. I presume you meant B. If so: > B[!C] <- 0 > B      [,1] [,2] [,3] [,4] [1,]    1    6    0    0 [2,]    2    0    0    0 [3,]    3    0    0    0 [4,]    4    0    0   19 [5,]    5    0    0    0 Cheers, Bert On Sat, Sep 5, 2020 at 11:18 AM Vivek Sutradhara <[hidden email]> 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]] > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.