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:
matrix similar line sum

Subject: matrix similar line sum

From: Bruno

Date: 2 Feb, 2009 23:30:20

Message: 1 of 5

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.

Subject: matrix similar line sum

From: Roger Stafford

Date: 3 Feb, 2009 00:08:01

Message: 2 of 5

"Bruno " <bruno.marchesi@gmail.com> wrote in message <gm7vmc$n1s$1@fred.mathworks.com>...
> .....
> 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.
> ......

  Hint: 1) use 'unique' with 'rows' option on first 3 columns of 'o', 2) use 'accumarray' to accumulate sums using 3rd output from 'unique' and 4th col. of 'o'; 3) concatenate results.

Roger Stafford

Subject: matrix similar line sum

From: Matt Fig

Date: 3 Feb, 2009 00:10:03

Message: 3 of 5

[t idxf] = unique(o(:,1:3),'rows','first');
[t idxl] = unique(o(:,1:3),'rows','last');
tmp = [0; cumsum(o(:,4))]
desired = [t(:,1:3), tmp(idxl+1)-tmp(idxf)]

Also, there have been several problems of this type in the last few weeks if you want to look for other discussions.




[dWGcmm[OOmmtmmVmg.OS{SPOQVU](Y]\bm;Z\Z]b]ObO]QWSSVOt]3P^^c

Subject: matrix similar line sum

From: John D'Errico

Date: 3 Feb, 2009 09:26:02

Message: 4 of 5

"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

Subject: matrix similar line sum

From: Bruno

Date: 3 Feb, 2009 11:56:01

Message: 5 of 5

Roger, Matt, John,

thanks for the time to answer the thread.

All the solutions worked indeed.

@Matt:

I'm really sorry for multiple threading this kind of problem.
I did some research before posting, but was unable to find specific
answers. I suppose I'll try better next time...

Thanks again,
Bruno.

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