Path: news.mathworks.com!not-for-mail From: "John D'Errico" <woodchips@rochester.rr.com> Newsgroups: comp.soft-sys.matlab Subject: Re: matrix similar line sum Date: Tue, 3 Feb 2009 09:26:02 +0000 (UTC) Organization: John D'Errico (1-3LEW5R) Lines: 52 Message-ID: <gm92ja$jio$1@fred.mathworks.com> References: <gm7vmc$n1s$1@fred.mathworks.com> Reply-To: "John D'Errico" <woodchips@rochester.rr.com> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1233653162 20056 172.30.248.37 (3 Feb 2009 09:26:02 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Tue, 3 Feb 2009 09:26:02 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 869215 Xref: news.mathworks.com comp.soft-sys.matlab:515620 "Bruno " <bruno.marchesi@gmail.com> wrote in message <gm7vmc$n1s$1@fred.mathworks.com>... > Greetings, > > please consider the array: > > o = > > 1 2 5 4 > 1 3 2 9 > 1 3 2 4 > 2 3 4 10 > 2 3 4 7 > 2 3 4 5 > 5 1 2 8 > 5 1 2 2 > 5 1 2 9 > 5 2 6 8 > > note that the 1st three columns are already sorted. > > The pursued function should take the array o and output an array y. > Each line of o where the 1st 3 elements are equal to > another line of o, the 4th is summed, but not the others. > For example, the expected answer y for the array o should be: > > 1 2 5 4 > 1 3 2 13 > 2 3 4 22 > 5 1 2 19 > 5 2 6 8 > > Is the problem clear? > > I sniff it would use find, sortrows, ismember, but I just can't > get it done. It doesn't have to be solved in one line, but avoiding > indexing/loop is surely good enough. SOS please ? > > Thanks, > Bruno. I would use my consolidator function on the problem. Apply it to the first three rows, forcing it to sum the 4th column as the dependent value. http://www.mathworks.com/matlabcentral/fileexchange/8354 [ou,osum] = consolidator(o(:,1:3),o(:,4),@sum); oresult = [ou,osum]; HTH, John