Thread Subject: find indices when using accumarray

Subject: find indices when using accumarray

From: Wolfgang Schwanghart

Date: 10 Sep, 2007 14:56:38

Message: 1 of 4

Dear community,

I got a problem using accumarray

ind = [1 2 3 1 2 3 1 2 3]';
val = [0.1:0.1:0.9]';
A = accumarray(ind,val,[3 1],@max)
A =

    0.7000
    0.8000
    0.9000

Now I don't want the maximum value but the index in val,
where the maximum is found. So I wrote my own function...
_________________________
function ix = findmax(s);
[m,ix] = max(s);
_________________________

... and entered:

A = accumarray(ind,val,[3 1],@findmax)

A =

     1
     2
     2

I don't know what A is telling me now but obviously it is
not what I wanted. I want A to be

A = [7 8 9]';

Is there a way to do so (without using sparse matrices?)

Best regards and thanks,
Wolfgang

Subject: find indices when using accumarray

From: Peter Perkins

Date: 10 Sep, 2007 19:14:19

Message: 2 of 4

Wolfgang Schwanghart wrote:

> A = accumarray(ind,val,[3 1],@findmax)
>
> A =
>
> 1
> 2
> 2
>
> I don't know what A is telling me now but obviously it is
> not what I wanted.

It may not be what you wanted, but it is waht you asked for. To see why, do this:

 >> A = accumarray(ind,val,[3 1],@(x) {x})
A =
     [3x1 double]
     [3x1 double]
     [3x1 double]
 >> A{:}
ans =
           0.7
           0.4
           0.1
ans =
           0.5
           0.8
           0.2
ans =
           0.3
           0.9
           0.6


accumarray has returned the index _within the first group_ of the max of the
first group, and so on.


> I want A to be
>
> A = [7 8 9]';

You're asking accumarray to compute a function that cannot be computed using
only using the values in each of the groups -- to compute what you want, you
also need to know the positions in the original data, and that information is
lost by the time the information gets to your findmax function.

You could take the output that you got using @max, and do a series of finds on
the original data.

Or, and I'm not saying that I recommend this, here's a too-clever way to use an
input that encodes both the value, and the position:

val2 = complex(val,(1:9)');
 >> A = accumarray(ind,val2,[3 1],@tmp)
A =
      7
      8
      9



where

function y = tmp(x)
[dum,i] = max(real(x));
y = imag(x(i));

Subject: find indices when using accumarray

From: Ilya Rozenfeld

Date: 3 Mar, 2008 16:10:27

Message: 3 of 4

I saw your question on the Loren's blog but I decided to
answer here since it seems to be a more proper forum.
Anyway, how about something like this


A = accumarray(ind, 1:numel(val) ,[3 1], @(x) findmax
(x,val))

where

function ix = findmax(indx, s)
[m,ix] = max(s(indx));
ix = indx(ix);




"Wolfgang Schwanghart" <schwanghart@googlemail.com> wrote
in message <fc3lv6$ovb$1@fred.mathworks.com>...
> Dear community,
>
> I got a problem using accumarray
>
> ind = [1 2 3 1 2 3 1 2 3]';
> val = [0.1:0.1:0.9]';
> A = accumarray(ind,val,[3 1],@max)
> A =
>
> 0.7000
> 0.8000
> 0.9000
>
> Now I don't want the maximum value but the index in val,
> where the maximum is found. So I wrote my own function...
> _________________________
> function ix = findmax(s);
> [m,ix] = max(s);
> _________________________
>
> ... and entered:
>
> A = accumarray(ind,val,[3 1],@findmax)
>
> A =
>
> 1
> 2
> 2
>
> I don't know what A is telling me now but obviously it is
> not what I wanted. I want A to be
>
> A = [7 8 9]';
>
> Is there a way to do so (without using sparse matrices?)
>
> Best regards and thanks,
> Wolfgang
>

Subject: find indices when using accumarray

From: Wolfgang Schwanghart

Date: 3 Mar, 2008 21:25:14

Message: 4 of 4

Hi Ilya,

I am deeply impressed. Thanks a lot, this worked out.

Best regards,
Wolfgang
 

"Ilya Rozenfeld" <rozeni.nospam@alum.rpi.edu> wrote in
message <fqh7tj$oea$1@fred.mathworks.com>...
> I saw your question on the Loren's blog but I decided to
> answer here since it seems to be a more proper forum.
> Anyway, how about something like this
>
>
> A = accumarray(ind, 1:numel(val) ,[3 1], @(x) findmax
> (x,val))
>
> where
>
> function ix = findmax(indx, s)
> [m,ix] = max(s(indx));
> ix = indx(ix);
>
>
>
>
> "Wolfgang Schwanghart" <schwanghart@googlemail.com> wrote
> in message <fc3lv6$ovb$1@fred.mathworks.com>...
> > Dear community,
> >
> > I got a problem using accumarray
> >
> > ind = [1 2 3 1 2 3 1 2 3]';
> > val = [0.1:0.1:0.9]';
> > A = accumarray(ind,val,[3 1],@max)
> > A =
> >
> > 0.7000
> > 0.8000
> > 0.9000
> >
> > Now I don't want the maximum value but the index in val,
> > where the maximum is found. So I wrote my own function...
> > _________________________
> > function ix = findmax(s);
> > [m,ix] = max(s);
> > _________________________
> >
> > ... and entered:
> >
> > A = accumarray(ind,val,[3 1],@findmax)
> >
> > A =
> >
> > 1
> > 2
> > 2
> >
> > I don't know what A is telling me now but obviously it is
> > not what I wanted. I want A to be
> >
> > A = [7 8 9]';
> >
> > Is there a way to do so (without using sparse matrices?)
> >
> > Best regards and thanks,
> > Wolfgang
> >
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix manipula... Wolfgang Schwanghart 10 Sep, 2007 12:03:54
rssFeed for this Thread

Contact us at files@mathworks.com