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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:29:51+00:00 2026-06-07T02:29:51+00:00

Lets have matrix A say A = magic(100); . I have seen 2 ways

  • 0

Lets have matrix A say A = magic(100);. I have seen 2 ways of computing sum of all elements of matrix A.

sumOfA = sum(sum(A));

Or

sumOfA = sum(A(:));

Is one of them faster (or better practise) then other? If so which one is it? Or are they both equally fast?

  • 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-07T02:29:52+00:00Added an answer on June 7, 2026 at 2:29 am

    It seems that you can’t make up your mind about whether performance or floating point accuracy is more important.

    If floating point accuracy were of paramount accuracy, then you would segregate the positive and negative elements, sorting each segment. Then sum in order of increasing absolute value. Yeah, I know, its more work than anyone would do, and it probably will be a waste of time.

    Instead, use adequate precision such that any errors made will be irrelevant. Use good numerical practices about tests, etc, such that there are no problems generated.

    As far as the time goes, for an NxM array,

    sum(A(:)) will require N*M-1 additions.

    sum(sum(A)) will require (N-1)*M + M-1 = N*M-1 additions.

    Either method requires the same number of adds, so for a large array, even if the interpreter is not smart enough to recognize that they are both the same op, who cares?

    It is simply not an issue. Don’t make a mountain out of a mole hill to worry about this.

    Edit: in response to Amro’s comment about the errors for one method over the other, there is little you can control. The additions will be done in a different order, but there is no assurance about which sequence will be better.

    A = randn(1000);
    format long g
    

    The two solutions are quite close. In fact, compared to eps, the difference is barely significant.

    sum(A(:))
    ans =
              945.760668102446
    
    sum(sum(A))
    ans =
              945.760668102449
    
    sum(sum(A)) - sum(A(:))
    ans =
          2.72848410531878e-12
    
    eps(sum(A(:)))
    ans =
          1.13686837721616e-13
    

    Suppose you choose the segregate and sort trick I mentioned. See that the negative and positive parts will be large enough that there will be a loss of precision.

    sum(sort(A(A<0),'descend'))
    ans =
              -398276.24754782
    
    sum(sort(A(A<0),'descend')) + sum(sort(A(A>=0),'ascend'))
    ans =
                945.7606681037
    

    So you really would need to accumulate the pieces in a higher precision array anyway. We might try this:

    [~,tags] = sort(abs(A(:)));
    sum(A(tags))
    ans =
              945.760668102446
    

    An interesting problem arises even in these tests. Will there be an issue because the tests are done on a random (normal) array? Essentially, we can view sum(A(:)) as a random walk, a drunkard’s walk. But consider sum(sum(A)). Each element of sum(A) (i.e., the internal sum) is itself a sum of 1000 normal deviates. Look at a few of them:

    sum(A)
    ans =
      Columns 1 through 6
             -32.6319600960983          36.8984589766173          38.2749084367497          27.3297721091922          30.5600109446534          -59.039228262402
      Columns 7 through 12
              3.82231962760523          4.11017616179294         -68.1497901792032          35.4196443983385          7.05786623564426         -27.1215387236418
      Columns 13 through 18
    

    When we add them up, there will be a loss of precision. So potentially, the operation as sum(A(:)) might be slightly more accurate. Is it so? What if we use a higher precision for the accumulation? So first, I’ll form the sum down the columns using doubles, then convert to 25 digits of decimal precision, and sum the rows. (I’ve displayed only 20 digits here, leaving 5 digits hidden as guard digits.)

    sum(hpf(sum(A)))
    ans =
    945.76066810244807408
    

    Or, instead, convert immediately to 25 digits of precision, then summing the result.

    sum(hpf(A(:))
    945.76066810244749807
    

    So both forms in double precision were equally wrong here, in opposite directions. In the end, this is all moot, since any of the alternatives I’ve shown are far more time consuming compared to the simple variations sum(A(:)) or sum(sum(A)). Just pick one of them and don’t worry.

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

Sidebar

Related Questions

Let's say I have a matrix with all 0's with the exception of the
I have a matrix lets say: a = 401.4800 344.0900 305.0300 462.2100 310.0600 397.3400
Lets say I have a graph G with its adjacency matrix A. I know
Okay, so basically lets say i have a matrix: matrix([[0, 1, 2, 3, 4],
Let's say I have created the following matrix: > x <- matrix(1:20000,nrow=100) > x[1:10,1:10]
I have a matrix in MATLAB, lets say: a = [ 89 79 96
So let's say I have a matrix, mdat and I only know the index
Lets say have this immutable record type: public class Record { public Record(int x,
I have lets say a WeeklyViewUserControl.xaml and a DailyViewUserControl.xaml. Normally I used stuff like
Lets say we have a table here, populated with the following data: acc_id1 acc_id2

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.