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 6857075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:58:09+00:00 2026-05-27T01:58:09+00:00

I am running a for loop with two matrices. One matrix(A) has ~100 strings

  • 0

I am running a for loop with two matrices. One matrix(A) has ~100 strings (such as, name1, name2, …, name100) and only has one column. The other matrix(B) is bigger than A with rows and columns of both values and strings. In some places in B matrix, each name of A matrix is matched. I would like to extract and stack matched entire rows with a particular string of matrix A on output matrix.

So, I am running as below,

output <- NULL
for(K in 1:nrow(A)){
  print(K)
  for(cc in 1:nrow(B)){
    for(dd in 1:ncol(B)){
      if(toupper(A[K])==toupper(B[cc,dd])){
        output <- rbind(output,B[cc,])
      }
    }
  }
}

But it is too slow. How do you make this for loop more efficient in terms of running time?

  • 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-27T01:58:10+00:00Added an answer on May 27, 2026 at 1:58 am

    Here some idea:

    A <- matrix(c('a','b','c','d'), ncol=1)
    B <- data.frame(z1=c('a','g','f','c'), z2=rnorm(4), z3=c('a','b','f','f'))
    
    id <- apply(B, 2, function(x) A %in% x)
    newB <- B[apply(id,1,sum)>0, ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running double forloop for two matrices. but one matrix has around 90,000
I have a program in Octave that has a loop - running a function
I'm running some JUnit tests on my applications. Every test has a for loop
I have created one testing app for running deep counter loop. I run the
I currently have my project running two separate threads (one for MFC operations, like
I have a program running a loop I want to have two time counters,
I have two linqTOsql entities that has a parent and child relationship, one to
I running into an infinite loop problem. I have two numeric up/down controls (Height
I have two while loops running one after the other (not inside of each
How do you end a long running Lua script? I have two threads, one

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.