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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:05:49+00:00 2026-05-24T03:05:49+00:00

I have a matrix (1051*1051) that has 0zeros along its diagonal and values greater

  • 0

I have a matrix (1051*1051) that has 0zeros along its diagonal and values greater than 0zero everywhere else. The goal is to conditionally reassign some values in the matrix. For example, the criteria I would like to implement is this: If any element is greater than, say, 400, then that row/column element will be assigned a 0zero value.

This is how my code is setup as of now:

dl <- 400       # condition

for( i in 1:dim(DIST)[1] ) {
    for( j in 1:dim(DIST)[1] ) {
        if( DIST[i,j] > dl ) {
             DIS[i,j] <- 0
        }
    }
}

DIST is the original matrix (1051*1051).

DIS is the copy of DIST and to be edited.

My question:
Is there any other way to do this? A faster way?

I have read that loops in R should be avoided. If anyone has a more efficient way please share.

Thank you.

  • 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-24T03:05:51+00:00Added an answer on May 24, 2026 at 3:05 am

    Just use [] assignment:

    DIST[DIST>400] <- 0
    

    See ?'[' for how this works. The key is that DIST>400 produces a logical vector of length length(DIST) (the number of elements in DIST), consisting of TRUE if the element is >400 and FALSE otherwise. That vector is then used to subset the matrix, and only the selected elements get assigned to.

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

Sidebar

Related Questions

Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix that stores x, y and z values as so: {x1,
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix of values some of which are -Inf. How can I
I have a matrix that I am showing in html table. I have my
I have a matrix which has multiple rows per ID. I need to extract
I have a matrix that is mapped to a .NET web service via the
I have a matrix M and a matrix L that contains 'pair of rows
I have a matrix which has the following form: M = [1 4 56
I have a matrix called (b2) that contains 3565 rows and 125 columns with

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.