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

  • Home
  • SEARCH
  • 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 7087311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:39:31+00:00 2026-05-28T07:39:31+00:00

As fast as possible, I would like to replace the first zeros in some

  • 0

As fast as possible, I would like to replace the first zeros in some rows of a matrix with values stored in another vector.

There is a numeric matrix where each row is a vector with some zeros.
I also have two vectors, one containing the rows, in what to be replaced, and another the new values: replace.in.these.rows and new.values. Also, I can generate the vector of first zeroes with sapply

mat <- matrix(1,5,5)
mat[c(1,8,10,14,16,22,14)] <- 0
replace.in.these.rows <- c(1,2,3)
new.values <- c(91,92,93)

corresponding.poz.of.1st.zero <- sapply(replace.in.these.rows, 
                                        function(x) which(mat [x,] == 0)[1] )

Now I would like something that iterates over the index vectors, but without a for loop possibly:

matrix[replace.in.these.rows, corresponding.poz.of.the.1st.zero ] <- new.values 

Is there a trick with indexing more than simple vectors? It could not use list or array(e.g.-by-column) as index.

By default R matrices are a set of column vectors. Do I gain anything if I store the data in a transposed form? It would mean to work on columns instead of rows.


Context:

This matrix stores contact ID-s of a network. This is not an adjacency matrix n x n, rather n x max.number.of.partners (or n*=30) matrix.

The network uses edgelist by default, but I wanted to store the “all links from X” together.

I assumed, but not sure if this is more efficient than always extract the information from the edgelist (multiple times each round in a simulation)

I also assumed that this linearly growing matrix form is faster than storing the same information in a same formatted list.

Some comments on these contextual assumptions are also welcome.

  • 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-28T07:39:32+00:00Added an answer on May 28, 2026 at 7:39 am

    Edit: If only the first zeros are to be replace then this approach works:

    first0s <-apply(mat[replace.in.these.rows, ] , 1, function(x) which(x==0)[1])
    mat[cbind(replace.in.these.rows, first0s)] <- new.values
    > mat
         [,1] [,2] [,3] [,4] [,5]
    [1,]   91    1    1    0    1
    [2,]    1    1    1    1   92
    [3,]    1   93    1    1    1
    [4,]    1    1    0    1    1
    [5,]    1    0    1    1    1
    

    Edit: I thought that the goal was to replace all zeros in the chosen rows and this was the approach. A completely vectorized approach:

     idxs <- which(mat==0, arr.ind=TRUE)
    # This returns that rows and columns that identify the zero elements
    # idxs[,"row"] %in% replace.in.these.rows
    #  [1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE
    # That isolates the ones you want.
    # idxs[ idxs[,"row"] %in% replace.in.these.rows , ]
    # that shows what you will supply as the two column argument to "["
    #     row col
    #[1,]   1   1
    #[2,]   3   2
    #[3,]   1   4
    #[4,]   2   5
     chosen.ones <- idxs[ idxs[,"row"] %in% replace.in.these.rows , ]
     mat[chosen.ones] <- new.values[chosen.ones[,"row"]]
    # Replace the zeros with the values chosen (and duplicated if necessary) by "row".
     mat
     #---------    
     [,1] [,2] [,3] [,4] [,5]
    [1,]   91    1    1   91    1
    [2,]    1    1    1    1   92
    [3,]    1   93    1    1    1
    [4,]    1    1    0    1    1
    [5,]    1    0    1    1    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to genrate a random permutation as fast as possible. The problem:
I would like to repeatedly execute a subprocess as fast as possible. However, sometimes
I like to keep my code as DRY as possible and would like to
I have a case here that I would like to have some opinions from
I would like to create a fast lightweight function in C language that returns
I would like to ask how is it possible for me to create a
I'm building a message router and would like it to be as fast as
I would like confirmation that my approach is extremely fast and appropriate for cross
I would like to do a lot of inserts, but could it be possible
I am currently implementing a Japanese dictionary and would like some ideas on how

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.