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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:35:46+00:00 2026-05-25T13:35:46+00:00

I am a new R-user and I have a kind of algorithm problem. I

  • 0

I am a new R-user and I have a kind of algorithm problem. I made some research on the web and on Stackoverflow, but can’t find my answer.

I have a squared matrix, for example :

  A B C D
A 0 0 0 1
B 0 1 1 0
C 1 0 0 0
D 0 1 1 1

This matrix represents links between keywords (A, B, C and D here). A ‘1’ (or a TRUE) means keywords are in relation. For example, the ‘1’ on the first row means A is linked to D.

I need to find the two most linked keywords on the matrix. I know I need to compute the scalar product between each row and the initial matrix. Then I take the sum of the rows and get the maximum.
But, what is the R program which put in a new matrix the product between each row of my matrix, and the matrix itself ?

Thanks!

  • 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-25T13:35:47+00:00Added an answer on May 25, 2026 at 1:35 pm

    I thought I had a cleverer answer but it turns out to be slower …

    tmp1 <- function(a) {
      n <- nrow(a)
      aa <- apply(array(apply(a,1,"*",a),
                        rep(n,3)),3,rowSums)
      apply(aa,2,which.max)
    }
    

    Previous solution:

    tmp2 <- function(a) {
      n <- nrow(a)
      r <- numeric(n)
      for(i in seq(n)) {
        b <- rowSums(a[i,]*a)
        r[i] <- which.max(b)
      }
      r
    }
    

    Test this on something reasonably large:

    n <- 50
    a <- matrix(0,nrow=n,ncol=n)
    a[sample(length(a),size=n^2/5,replace=TRUE)] <- 1 
    
    all(tmp1(a)==tmp2(a)) ## TRUE
    
    library(rbenchmark)
    benchmark(tmp1(a),tmp2(a))
    > benchmark(tmp1(a),tmp2(a))
         test replications elapsed relative user.self sys.self 
    1 tmp1(a)          100   4.030 9.264368     2.052     1.96  
    2 tmp2(a)          100   0.435 1.000000     0.232     0.20  
    

    You will presumably do even better if you can do it in terms of sparse matrices.

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

Sidebar

Related Questions

I have got a form which a user can use to create a new
I have this block of code: users = Array.new users << User.find(:all, :conditions =>
Hello I have a user model and a ratings model. Whenever a new user
I have such view that handles user registration. After creating new user i want
I have a database called ADB. When I create a new user/role, that user
I have following code class User attr_accessor :name end u = User.new u.name =
I have to start a new project where user authentication/management will be required. A
I have 2 radio button with values New & Existing. If user chooses New,
I have a webpage where I want the user to see a new image
I have a script which creates a user-defined object like this: obj = new

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.