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:
elementary sum question

Subject: elementary sum question

From: Pink Panther

Date: 29 Dec, 2010 14:22:04

Message: 1 of 5

say that
A = [ 2 2 2 -2]
so
sum(A) = 4
and also
sum(A>0) = 3 and sum(A<0) = 1

what should I write to get something like
sum (positive numbers of A) = 6 ie 2 + 2 + 2 instead of 3, the number of positive numbers in the matrix we get when using sum(A>0) ?

many thanks in advance

Subject: elementary sum question

From: Wayne King

Date: 29 Dec, 2010 14:34:04

Message: 2 of 5

"Pink Panther" wrote in message <iffg6c$qcs$1@fred.mathworks.com>...
> say that
> A = [ 2 2 2 -2]
> so
> sum(A) = 4
> and also
> sum(A>0) = 3 and sum(A<0) = 1
>
> what should I write to get something like
> sum (positive numbers of A) = 6 ie 2 + 2 + 2 instead of 3, the number of positive numbers in the matrix we get when using sum(A>0) ?
>
> many thanks in advance

Hi, one of many ways.

indices = find(A>0);
sum(A(indices))

Wayne

Subject: elementary sum question

From: dpb

Date: 29 Dec, 2010 14:34:28

Message: 3 of 5

Pink Panther wrote:
> say that A = [ 2 2 2 -2]
> so sum(A) = 4
> and also sum(A>0) = 3 and sum(A<0) = 1
>
> what should I write to get something like sum (positive numbers of A) =
> 6 ie 2 + 2 + 2 instead of 3, the number of positive numbers in the
> matrix we get when using sum(A>0) ?
> many thanks in advance

sum(A(A>0)

Use the _values_ of A that satisfy the condition, not the logical
indices of those entries that satisfy the condition. This is a general
principle, obviously.

--

Subject: elementary sum question

From: Pink Panther

Date: 29 Dec, 2010 14:41:04

Message: 4 of 5

dpb <none@non.net> wrote in message <iffgtk$ptl$2@news.eternal-september.org>...
> Pink Panther wrote:
> > say that A = [ 2 2 2 -2]
> > so sum(A) = 4
> > and also sum(A>0) = 3 and sum(A<0) = 1
> >
> > what should I write to get something like sum (positive numbers of A) =
> > 6 ie 2 + 2 + 2 instead of 3, the number of positive numbers in the
> > matrix we get when using sum(A>0) ?
> > many thanks in advance
>
> sum(A(A>0)
>
> Use the _values_ of A that satisfy the condition, not the logical
> indices of those entries that satisfy the condition. This is a general
> principle, obviously.
>
> --
great, many thanks

Subject: elementary sum question

From: dpb

Date: 29 Dec, 2010 14:46:16

Message: 5 of 5

Pink Panther wrote:
> dpb <none@non.net> wrote in message
> <iffgtk$ptl$2@news.eternal-september.org>...
>> Pink Panther wrote:
>> > say that A = [ 2 2 2 -2]
>> > so sum(A) = 4
>> > and also sum(A>0) = 3 and sum(A<0) = 1
>> > > what should I write to get something like sum (positive numbers of
>> A) = > 6 ie 2 + 2 + 2 instead of 3, the number of positive numbers in
>> the > matrix we get when using sum(A>0) ?
>> > many thanks in advance
>>
>> sum(A(A>0)
>>
>> Use the _values_ of A that satisfy the condition, not the logical
>> indices of those entries that satisfy the condition. This is a
>> general principle, obviously.
>>
>> --
> great, many thanks

BTW, for clarity, try the two expressions at the command window--

A>0

A(A>0)

and note the difference.

--

Tags for this Thread

No tags are associated with 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