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:
sum of each elements in a matrix

Subject: sum of each elements in a matrix

From: Shreeshail

Date: 23 Jun, 2011 05:42:04

Message: 1 of 8

is there any separate in built function to add all elements in a matrix to obtain a single value.?

Subject: sum of each elements in a matrix

From: Nasser M. Abbasi

Date: 23 Jun, 2011 06:04:18

Message: 2 of 8

On 6/22/2011 10:42 PM, Shreeshail wrote:
> is there any separate in built function to add all elements in a matrix to
>obtain a single value.?

maybe sum(A(:)) ?

--Nasser

Subject: sum of each elements in a matrix

From: Krishna Kumar

Date: 23 Jun, 2011 06:19:02

Message: 3 of 8

"Shreeshail " <newsreader@mathworks.com> wrote in message <itujnc$pa8$1@newscl01ah.mathworks.com>...
> is there any separate in built function to add all elements in a matrix to obtain a single value.?

Hi Shreeshail,
 Sum(Sum(X)) will suffice. If you dont want that, you can try these
sum(reshape(X,prod(size(X),1);
or
sum(unique(X))--->if the data has no repitition.
Hth
Krishna

Subject: sum of each elements in a matrix

From: Krishna Kumar

Date: 23 Jun, 2011 06:23:02

Message: 4 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <itul12$h54$1@speranza.aioe.org>...
> On 6/22/2011 10:42 PM, Shreeshail wrote:
> > is there any separate in built function to add all elements in a matrix to
> >obtain a single value.?
>
> maybe sum(A(:)) ?
>
> --Nasser
>
Yes this is better than my suggestion. It works same as reshape, but is cool.
Krishna.

Subject: sum of each elements in a matrix

From: Anon

Date: 23 Jun, 2011 09:50:19

Message: 5 of 8

Krishna,

sum(unique(X)) <--

this is a joke, isn't it! How about
sum(flipud(log(exp(sort(X(:)*2/2))))
Might be even more inefficient...

Lol, Anon

"Krishna Kumar" <skrishnakumar.accet@gmail.com> wrote in message <itulsm$10b$1@newscl01ah.mathworks.com>...
> "Shreeshail " <newsreader@mathworks.com> wrote in message <itujnc$pa8$1@newscl01ah.mathworks.com>...
> > is there any separate in built function to add all elements in a matrix to obtain a single value.?
>
> Hi Shreeshail,
> Sum(Sum(X)) will suffice. If you dont want that, you can try these
> sum(reshape(X,prod(size(X),1);
> or
> sum(unique(X))--->if the data has no repitition.
> Hth
> Krishna

Subject: sum of each elements in a matrix

From: Krishna Kumar

Date: 23 Jun, 2011 10:26:04

Message: 6 of 8

"Anon" wrote in message <itv28r$116$1@newscl01ah.mathworks.com>...
> Krishna,
>
> sum(unique(X)) <--
>
> this is a joke, isn't it! How about
> sum(flipud(log(exp(sort(X(:)*2/2))))
> Might be even more inefficient...
>
> Lol, Anon
Hi Anon,
Suppose you didnt get it. Of course Abbasi suggestion was the right one,which I didnt know before. Anyhow this
sum(unique(X)) is useful since it converts the matrix into a vector. It is certainly not as ridiculous as you Think! :)

Subject: sum of each elements in a matrix

From: Matt J

Date: 23 Jun, 2011 13:19:04

Message: 7 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <itul12$h54$1@speranza.aioe.org>...
> On 6/22/2011 10:42 PM, Shreeshail wrote:
> > is there any separate in built function to add all elements in a matrix to
> >obtain a single value.?
>
> maybe sum(A(:)) ?
=====================

Careful! For large non-double arrays, you need to do

sum(A(:),'double')

to avoid overflow issues.

Subject: sum of each elements in a matrix

From: Sean de

Date: 23 Jun, 2011 14:18:05

Message: 8 of 8

"Krishna Kumar" <skrishnakumar.accet@gmail.com> wrote in message <itv4bs$5tu$1@newscl01ah.mathworks.com>...
> "Anon" wrote in message <itv28r$116$1@newscl01ah.mathworks.com>...
> > Krishna,
> >
> > sum(unique(X)) <--
> >
> > this is a joke, isn't it! How about
> > sum(flipud(log(exp(sort(X(:)*2/2))))
> > Might be even more inefficient...
> >
> > Lol, Anon
> Hi Anon,
> Suppose you didnt get it. Of course Abbasi suggestion was the right one,which I didnt know before. Anyhow this
> sum(unique(X)) is useful since it converts the matrix into a vector. It is certainly not as ridiculous as you Think! :)

Computationally unique is like a 50cc scooter next to a Corvette!

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