From: Samuel Mathew on
Assuming I have a matrix such as

Index Val1 Val2 Val3
a = [1 2 5 4
1 0 2 6
1 2 1 2
2 5 4 7
2 12 0 1
3 4 7 5
3 1 0 1
3 1 1 2]

I need to add the column values within the matrix depending on the index value (i.e. all rows with index value 1 get added together, all with 2 ... etc.)

in order to get a matrix

a = [1 4 8 12
2 17 4 8
3 6 8 8]

Thanks

sam

P.S - if it can be automated it would be a lot better - as my matrix row size is in the 100s
From: Steven Lord on

"Samuel Mathew" <msam313(a)hotmail.com> wrote in message
news:hruhva$dhc$1(a)fred.mathworks.com...
> Assuming I have a matrix such as
> Index Val1 Val2 Val3
> a = [1 2 5 4 1 0 2 6
> 1 2 1 2 2 5 4 7
> 2 12 0 1
> 3 4 7 5
> 3 1 0 1
> 3 1 1 2]
>
> I need to add the column values within the matrix depending on the index
> value (i.e. all rows with index value 1 get added together, all with 2 ...
> etc.)
>
> in order to get a matrix
>
> a = [1 4 8 12
> 2 17 4 8
> 3 6 8 8]

Use ACCUMARRAY.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Matt J on
help accumarray
From: Samuel Mathew on
Thank you steven,

but I can't seem to understand the help files on ACCUMARRAY - how does it work??

thanks

"Steven Lord" <slord(a)mathworks.com> wrote in message <hruio3$610$1(a)fred.mathworks.com>...
>
> "Samuel Mathew" <msam313(a)hotmail.com> wrote in message
> news:hruhva$dhc$1(a)fred.mathworks.com...
> > Assuming I have a matrix such as
> > Index Val1 Val2 Val3
> > a = [1 2 5 4 1 0 2 6
> > 1 2 1 2 2 5 4 7
> > 2 12 0 1
> > 3 4 7 5
> > 3 1 0 1
> > 3 1 1 2]
> >
> > I need to add the column values within the matrix depending on the index
> > value (i.e. all rows with index value 1 get added together, all with 2 ...
> > etc.)
> >
> > in order to get a matrix
> >
> > a = [1 4 8 12
> > 2 17 4 8
> > 3 6 8 8]
>
> Use ACCUMARRAY.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>