From: Mustafa on
Hello,

I am looking for something that does the following. suppose you have

ind = [ 1 2 3 2 5 7 7 7]
val = [ 4 2 4 3 3 3 4 4]

I want to some the maximums for each index (1 to 7), so the answer would be

ans = [ 4 3 4 0 3 8]

note that index 7's answer is 8 because it has two maximum (4 and 4).

I know how to do it in a for loop buy i am looking for something more optimized. Thanks!
From: Bruno Luong on
"Mustafa " <Mustafa.Geoscientist(a)live.com> wrote in message <i315os$ns7$1(a)fred.mathworks.com>...
> Hello,
>
> I am looking for something that does the following. suppose you have
>
> ind = [ 1 2 3 2 5 7 7 7]
> val = [ 4 2 4 3 3 3 4 4]
>
> I want to some the maximums for each index (1 to 7), so the answer would be
>
> ans = [ 4 3 4 0 3 8]
>
> note that index 7's answer is 8 because it has two maximum (4 and 4).
>
> I know how to do it in a for loop buy i am looking for something more optimized. Thanks!

accumarray(ind(:),val(:),[],@(x) sum(x.*(x==max(x))))

Bruno
From: Mustafa on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i3175q$l49$1(a)fred.mathworks.com>...
> "Mustafa " <Mustafa.Geoscientist(a)live.com> wrote in message <i315os$ns7$1(a)fred.mathworks.com>...
> > Hello,
> >
> > I am looking for something that does the following. suppose you have
> >
> > ind = [ 1 2 3 2 5 7 7 7]
> > val = [ 4 2 4 3 3 3 4 4]
> >
> > I want to some the maximums for each index (1 to 7), so the answer would be
> >
> > ans = [ 4 3 4 0 3 8]
> >
> > note that index 7's answer is 8 because it has two maximum (4 and 4).
> >
> > I know how to do it in a for loop buy i am looking for something more optimized. Thanks!
>
> accumarray(ind(:),val(:),[],@(x) sum(x.*(x==max(x))))
>
> Bruno

Nice!! Thanks, it works perfectly. I used accumarray before and actually thought about it for this situation (but I didn't know you can define your own Anonymous function in it). Now i know, and i think it will come in handy alot ;)

Thanks again!