Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:03:40+00:00 2026-05-24T02:03:40+00:00

Thank you so much for your help in advance! I am trying to modify

  • 0

Thank you so much for your help in advance!

I am trying to modify an existing matrix such that, when a new line is added to the matrix, it removes values from the preexisting matrix.

For example, I have the matrix:

[,1] [,2] [,3] [,4]
 1     1    0    0
 0     1    0    0
 1     0    1    0
 0     0    1    1

I want to add another vector, I.vec, which has two values (I.vec=c(0,1,1,0)).
This is easy enough to do. I just rbind it to the matrix.
Now, for every column where I.vec is equal to 1, I want to randomly select a value from the other rows and make it zero.
Ideally, this would end up with a matrix like:

[,1] [,2] [,3] [,4]
 1     0    0    0
 0     1    0    0
 1     0    0    0
 0     0    1    1
 0     1    1    0

But each time I run the iteration, I want it to randomly sample again.

So this is what I have tried:

mat1<-matrix(c(1,1,0,0,0,1,0,0,1,0,1,0,0,0,1,1),byrow=T, nrow=4)
I.vec<-c(0,1,1,0)
mat.I<-rbind(mat1,I.vec)
mat.I.r<-mat.I
d1<-mat.I[,which(mat.I[5,]==1)]
mat.I.r[sample(which(d1[1:4]==1),1),which(mat.I[5,]==1)]<-0

But this only deletes one of the two values I would like to delete. I have also tried variations on subsetting the matrix, but I have not been successful.

Thank you again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:03:41+00:00Added an answer on May 24, 2026 at 2:03 am

    There is a little bit of ambiguity in the description from the OP, so two solutions are suggested:

    Assuming that only existing 1s in relevant columns can be set to 0

    I’ll just alter the original function (see below). The change is to the line defining rows. I now have (there was a bug in the original – the version below is revised to handle deal with the bug):

    rows <- sapply(seq_along(cols), 
                       function(x, mat, cols) {
                           ones <- which(mat[,cols[x]] == 1L)
                           out <- if(length(ones) == 1L) {
                                      ones
                                  } else {
                                      sample(ones, 1)
                           }
                           out
                       }, mat = mat, cols = cols)
    

    Basically, what this does is, for each column we need to swap a 1 to a 0, we work out which rows of the column contain 1s and sample one of these.

    Edit: We have to handle the case where there is only a single 1 in a column. If we just sample from a length 1 vector, R’s sample() will treat it as if we wanted to sample from the set seq_len(n) not from the length 1 set n. We handle this now with an if, else statement.

    We have to do this individually for each column so we get the correct rows. I suppose we could do some nice manipulation to avoid repeated calls to which() and sample(), but how escapes me at the moment, because we do have to handle the case where there is only one 1 in the column. Here’s the finished function (updated to handle the length 1 sample bug in the original):

    foo <- function(mat, vec) {
        nr <- nrow(mat)
        nc <- ncol(mat)
    
        cols <- which(vec == 1L)
        rows <- sapply(seq_along(cols), 
                       function(x, mat, cols) {
                           ones <- which(mat[,cols[x]] == 1L)
                           out <- if(length(ones) == 1L) {
                                      ones
                                  } else {
                                      sample(ones, 1)
                                  }
                           out
                       }, mat = mat, cols = cols)
    
        ind <- (nr*(cols-1)) + rows
        mat[ind] <- 0
    
        mat <- rbind(mat, vec)
        rownames(mat) <- NULL
    
        mat
    }
    

    and here it is in action:

    > set.seed(2)
    > foo(mat1, ivec)
         [,1] [,2] [,3] [,4]
    [1,]    1    0    0    0
    [2,]    0    1    0    0
    [3,]    1    0    1    0
    [4,]    0    0    0    1
    [5,]    0    1    1    0
    

    and it works when there is only one 1 in a column we want to do a swap in:

    > foo(mat1, c(0,0,1,1))
         [,1] [,2] [,3] [,4]
    [1,]    1    1    0    0
    [2,]    0    1    0    0
    [3,]    1    0    1    0
    [4,]    0    0    0    1
    [5,]    0    0    1    1
    

    Original Answer: Assuming any value in a relevant column can be set to zero

    Here is a vectorised answer, where we treat the matrix as a vector when doing the replacement. Using the example data:

    mat1 <- matrix(c(1,1,0,0,0,1,0,0,1,0,1,0,0,0,1,1), byrow = TRUE, nrow = 4)
    ivec <- c(0,1,1,0)
    
    ## Set a seed to make reproducible
    set.seed(2)
    
    ## number of rows and columns of our matrix
    nr <- nrow(mat1)
    nc <- ncol(mat1)
    
    ## which of ivec are 1L
    cols <- which(ivec == 1L)
    
    ## sample length(cols) row indices, with replacement
    ## so same row can be drawn more than once
    rows <- sample(seq_len(nr), length(cols), replace = TRUE)
    
    ## Compute the index of each rows cols combination
    ## if we treated mat1 as a vector
    ind <- (nr*(cols-1)) + rows
    ## ind should be of length length(cols)
    
    ## copy for illustration
    mat2 <- mat1
    
    ## replace the indices we want with 0, note sub-setting as a vector
    mat2[ind] <- 0
    
    ## bind on ivec
    mat2 <- rbind(mat2, ivec)
    

    This gives us:

    > mat2
         [,1] [,2] [,3] [,4]
            1    0    0    0
            0    1    0    0
            1    0    0    0
            0    0    1    1
    ivec    0    1    1    0
    

    If I were doing this more than once or twice, I’d wrap this in a function:

    foo <- function(mat, vec) {
        nr <- nrow(mat)
        nc <- ncol(mat)
    
        cols <- which(vec == 1L)
        rows <- sample(seq_len(nr), length(cols), replace = TRUE)
    
        ind <- (nr*(cols-1)) + rows
        mat[ind] <- 0
    
        mat <- rbind(mat, vec)
        rownames(mat) <- NULL
    
        mat
    }
    

    Which gives:

    > foo(mat1, ivec)
         [,1] [,2] [,3] [,4]
    [1,]    1    1    0    0
    [2,]    0    1    0    0
    [3,]    1    0    1    0
    [4,]    0    0    0    1
    [5,]    0    1    1    0
    

    If you wanted to do this for multiple ivecs, growing mat1 each time, then you probably don’t want to do that in a loop as growing objects is slow (it involves copies etc). But you could just modify the definition of ind to include the extra n rows you bind on for the n ivecs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thanks in advance for your help. I'm currently trying to create a w3c standards
Thank you very much in advance for reading. I'm programming a web application in
Hello and thank you in advance, I am very much a Django/Python noobie. I
Long-time follower, thanks so much for all your help over the years! I have
Several times I heard that PHP hosting is much cheaper than ASP.NET one. I
I keep hearing that branching in git is so much easier than in SVN,
I have noticed that the Windows Vista/7 dialogs look MUCH better than just a
What are the killer-features of hudson that make using it so much better than
I'm trying to create a view that's full of controls (buttons, text views, labels,
I'm new to Perl and playing around screen scraping and regex. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.