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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:55:39+00:00 2026-06-07T20:55:39+00:00

I have a triangular matrix and would like to loop through all the elements

  • 0

I have a triangular matrix and would like to loop through all the elements efficiently. Is there a smart way that I simply don’t see?

So here’s a small example of my matrix

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

What I want to do is perform some function f() on the elements in this matrix that are under (over) the main diagonal. (background: I know that I have a symmetric matrix and would like to perform some time consuming data manipulations and I’d like to ‘mirror’ my matrix)

  • 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-06-07T20:55:40+00:00Added an answer on June 7, 2026 at 8:55 pm

    As shown below, lower.tri() and upper.tri() provide an expressive (and fast) means of extracting and replacing elements in the relevant sectors a matrix. Since the function you’re applying to the elements is presumably slow compared to indexing operations, there’s probably no point in searching for faster indexing options (or in trying to avoid the single call to t()).

    ## Example data
    m <- matrix(c(1,2,3,4,0,1,7,4,0,0,1,10,0,0,0,1), ncol=4)
    
    ## Example of a slow function
    slowFun <- function(x) sapply(x, function(x) {Sys.sleep(0.1); x^2})
    
    ## Proposed strategy
    m[lower.tri(m)] <- slowFun(m[lower.tri(m)])
    m[upper.tri(m)] <- t(m)[upper.tri(m)]      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an upper triangular matrix on MATLAB LIKE A = 1 2
If I have this: A * f = g; A: upper triangular matrix (n
I have a string in c++ and it represents an upper triangular matrix, What
I would like to automatically determine all of the properties (including the hidden ones)
If I have a table (upper triangular matrix), data types are float, an example
I have some xml that looks like this: <rootElement attribute=' > '/> This is
I have some data that looks like this: +---+--------+-------------+---------------+--------------+ | | A | B
I have a a mesh, with certain types of elements (e.g. triangular, tetra). For
I searched the last hours for a custom control that looks like this: Triangular
Does python have a built-in function that converts a matrix into row echelon form

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.