Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Sum selected elements of a vector
Date: Fri, 15 Mar 2013 07:10:05 +0000 (UTC)
Organization: Erasmus MC
Lines: 23
Message-ID: <khuhgd$osp$1@newscl01ah.mathworks.com>
References: <khtl4u$aff$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1363331405 25497 172.30.248.38 (15 Mar 2013 07:10:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 15 Mar 2013 07:10:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:791207

"Jeff " <jeffong@rocketmail.com> wrote in message <khtl4u$aff$1@newscl01ah.mathworks.com>...
> Hi guys,
> 
> How can I sum the elements (row 1 + row 2 & row 3 + row 4) of a 4 x 1 vector and have a 2 x 1 vector as output?
> 
> The 'sum' function does not work as it will sum up all the elements and provide a single value. See example..
> 
> Thanks!
> 
> Example:
> 
> A=[row 1; row 2; row 3; row 4]
> 
> I am trying to obtain:
> 
> B=[row 1 + row 2; row 3 + row 4];

I would recommend something simple like

A = rand(4,1) ; 
B = [sum(A(1:2,:)) ; sum(A(3:4,:))] 

~ Jos