Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
newbie - sum elements of matrix.

Subject: newbie - sum elements of matrix.

From: arron

Date: 11 Apr, 2012 11:51:14

Message: 1 of 14

just a quick one, say i have a matrix

a = 111
       111
       111

how do i sum all the elements to get 9? ie add al the elements up? matlab gives me 3 3 3 when i use sum(a). This is not what I want.

Thanks

Subject: newbie - sum elements of matrix.

From: Nasser M. Abbasi

Date: 11 Apr, 2012 12:29:42

Message: 2 of 14

On 4/11/2012 6:51 AM, arron wrote:
> just a quick one, say i have a matrix
>
> a = 111
> 111
> 111
>
> how do i sum all the elements to get 9? ie add al the elements up? matlab gives
>me 3 3 3 when i use sum(a). This is not what I want.
>
> Thanks


EDU>> a =[ 1 1 1;
        1 1 1;
        1 1 1];
    
sum(a(:))


ans =

      9

Subject: newbie - sum elements of matrix.

From: Matt J

Date: 11 Apr, 2012 13:17:20

Message: 3 of 14

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jm3tfk$6n8$1@speranza.aioe.org>...
> On 4/11/2012 6:51 AM, arron wrote:
> > just a quick one, say i have a matrix
> >
> > a = 111
> > 111
> > 111
> >
> > how do i sum all the elements to get 9? ie add al the elements up? matlab gives
> >me 3 3 3 when i use sum(a). This is not what I want.
> >
> > Thanks
>
>
> EDU>> a =[ 1 1 1;
> 1 1 1;
> 1 1 1];
>
> sum(a(:))
>
>
> ans =
>
> 9


For large matrices, this may be safer against overflow,

sum(a(:),'double');

Subject: newbie - sum elements of matrix.

From: Zachary

Date: 11 Apr, 2012 15:05:26

Message: 4 of 14

"arron" wrote in message <jm3r7i$1b9$1@newscl01ah.mathworks.com>...
> just a quick one, say i have a matrix
>
> a = 111
> 111
> 111
>
> how do i sum all the elements to get 9? ie add al the elements up? matlab gives me 3 3 3 when i use sum(a). This is not what I want.
>
> Thanks


You can also just do

sum(sum(a))

Subject: newbie - sum elements of matrix.

From: Matt J

Date: 11 Apr, 2012 15:20:21

Message: 5 of 14

"Zachary" wrote in message <jm46jm$h34$1@newscl01ah.mathworks.com>...
>
>
> You can also just do
>
> sum(sum(a))
=====================

That's common, but less efficient, since you now have the overhead of two function calls and have to allocate memory to hold intermediate results.

Subject: newbie - sum elements of matrix.

From: Steven_Lord

Date: 11 Apr, 2012 15:29:55

Message: 6 of 14



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:jm47fl$l2k$1@newscl01ah.mathworks.com...
> "Zachary" wrote in message <jm46jm$h34$1@newscl01ah.mathworks.com>...
>>
>>
>> You can also just do sum(sum(a))
> =====================
>
> That's common, but less efficient, since you now have the overhead of two
> function calls and have to allocate memory to hold intermediate results.

Plus that doesn't do what the OP asked for in the case where a is an
N-dimensional array.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: newbie - sum elements of matrix.

From: Jan Simon

Date: 15 Apr, 2012 00:55:39

Message: 7 of 14

Dear Matt J,

Why does this help to avoid overflow:
  sum(a(:),'double');

For some Matlab releases and sizes of the matrix "sum(sum(a))" has been faster due to the multithreading of SUM. But modern releases split the "sum(a(:))" into chunks for multi-threading also.

Kind regards, Jan

Subject: newbie - sum elements of matrix.

From: Matt J

Date: 15 Apr, 2012 09:20:06

Message: 8 of 14

"Jan Simon" wrote in message <jmd6ab$hpe$1@newscl01ah.mathworks.com>...
> Dear Matt J,
>
> Why does this help to avoid overflow:
> sum(a(:),'double');

I guess I should have mentioned. If 'a' is a large array of type single (or anything less than double precision), then sum(a(:)) can result in overflow, e.g.,

>> a=rand(300,300,300,'single'); sum(a(:),'double')-sum(a(:))

ans =

  898.7078


> For some Matlab releases and sizes of the matrix "sum(sum(a))" has been faster due to the multithreading of SUM. But modern releases split the "sum(a(:))" into chunks for multi-threading also.
=============

That's interesting, but I don't see why multi-threading in any MATLAB version would apply exclusively to only one of the implementations. Since both use SUM, both should be multithreaded.

Subject: newbie - sum elements of matrix.

From: Bruno Luong

Date: 15 Apr, 2012 09:34:06

Message: 9 of 14

"Matt J" wrote in message <jme3s5$a6c$1@newscl01ah.mathworks.com>...
>
> That's interesting, but I don't see why multi-threading in any MATLAB version would apply exclusively to only one of the implementations. Since both use SUM, both should be multithreaded.

That's not difficult to see there is two levels of multi-threading:

1. When sum is applied on a matrix along a specific dimension, then each individual sum can parallelized. This does not affect the result, since the sum order is unchanged.

2. When sum is applied when a long vector, then the Matlab automatically splits the vector into N chunks, performs the sum on each sum in parallel, then add the results.

As I understand Jan correctly, earlier Matlab versions has only the first strategy (1). To me it seems like a temporary transition situation. There is no reason why sum(a(:)) should be slower than sum(sum(a)).

Bruno

Subject: newbie - sum elements of matrix.

From: Matt J

Date: 15 Apr, 2012 09:36:06

Message: 10 of 14

"Matt J" wrote in message <jme3s5$a6c$1@newscl01ah.mathworks.com>...
> "Jan Simon" wrote in message <jmd6ab$hpe$1@newscl01ah.mathworks.com>...
> > Dear Matt J,
> >
> > Why does this help to avoid overflow:
> > sum(a(:),'double');
>
> I guess I should have mentioned. If 'a' is a large array of type single (or anything less than double precision), then sum(a(:)) can result in overflow, e.g.,
>
> >> a=rand(300,300,300,'single'); sum(a(:),'double')-sum(a(:))
>
> ans =
>
> 898.7078
================

I guess that's a bad example, since the discrepancy is on the order of the floating point precision. In fact, I was trying to reproduce the overflow issue I found with MEAN in this thread last year

http://www.mathworks.com/matlabcentral/newsreader/view_thread/304835#826650

Incidentally, in R2011b, the issue seems to have gotten worse!! Now even triple calls to MEAN don't give robust results



>> a=rand(500,500,300,'single');

>> mean(a(:)),

ans =

    0.4474

>> mean(mean(mean(a(:)))),

ans =

    0.4474

>> sum(a(:))/numel(a),

ans =

    0.4474

>> sum(a(:),'double')/numel(a),

ans =

    0.5000

Subject: newbie - sum elements of matrix.

From: Matt J

Date: 15 Apr, 2012 09:56:07

Message: 11 of 14

"Matt J" wrote in message <jme4q6$dv2$1@newscl01ah.mathworks.com>...
>
>
> >> mean(mean(mean(a(:)))),
>
> ans =
>
> 0.4474
================

Sorry, false alarm. This should have been

>> mean(mean(mean(a))), %No Colon!!

ans =

    0.5000

So everything is fine. However, my last post still serves as an illustration of when sum(a(:),'double') is better.

Subject: newbie - sum elements of matrix.

From: Matt J

Date: 15 Apr, 2012 10:07:06

Message: 12 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jme4me$d98$1@newscl01ah.mathworks.com>...
>
> That's not difficult to see there is two levels of multi-threading:
=============

OK. That does make sense (but I wouldn't say it's "not difficult to see") :-)

Subject: newbie - sum elements of matrix.

From: Bruno Luong

Date: 15 Apr, 2012 10:18:09

Message: 13 of 14

"Matt J" wrote in message <jme6ka$k90$1@newscl01ah.mathworks.com>...
> (but I wouldn't say it's "not difficult to see") :-)

Fair enough. :-)

Subject: newbie - sum elements of matrix.

From: Jan Simon

Date: 16 Apr, 2012 00:23:09

Message: 14 of 14

Dear Bruno,

> > That's interesting, but I don't see why multi-threading in any MATLAB version would apply exclusively to only one of the implementations. Since both use SUM, both should be multithreaded.
>
> That's not difficult to see there is two levels of multi-threading:

Exactly. A sum of a matrix can be split in two ways: either the columns are processed by different threads, or the columns are split into chunks and the partial sums are added finally.
In 2009a SUM split vectors with > 89000 elements into chunks for multi-threading, see:
  http://www.mathworks.com/matlabcentral/newsreader/view_thread/274419
Unfortunately the temporary results have been added in the order of their temporal appearance, such the results could differ slightly from run to run:
  http://www.mathworks.de/matlabcentral/newsreader/view_thread/261678
Since 2009B the partial sums have a fixed order, such that the SUM is reliable now.

Once I gave the advice to use SUM(X(:)) in the FEX, but as far as I remember John D'Errico mentioned the benefit of SUM(SUM(X)). I cannot find the submission currently.

Matlab 2009a/64, Win7, Core2Duo (2 cores):
  x = rand(1e4);
  tic; for i=1:10; v=sum(x(:)); end; toc
  % Elapsed time is 2.080143 seconds.
  tic; for i=1:10; v=sum(sum(x)); end; toc
  % Elapsed time is 1.542963 seconds.

Matlab 2011b/64:
  % Elapsed time is 1.541947 seconds.
  % Elapsed time is 1.570768 seconds.

But for both Matlab releases the processor load is 98%-100% during these computations. It seems like 2009a has a multi-threaded SUM for vectors also, but with a false cache sharing or a similar problem.

I admit, this topic is less interesting today in the post-2009a days.

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us