Path: news.mathworks.com!not-for-mail
From: Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: find indices when using accumarray
Date: Mon, 10 Sep 2007 15:14:19 -0400
Organization: The MathWorks, Inc.
Lines: 69
Message-ID: <fc452b$8mr$1@fred.mathworks.com>
References: <fc3lv6$ovb$1@fred.mathworks.com>
NNTP-Posting-Host: perkinsp.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1189451659 8923 144.212.219.29 (10 Sep 2007 19:14:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 10 Sep 2007 19:14:19 +0000 (UTC)
User-Agent: Thunderbird 2.0.0.6 (Windows/20070728)
In-Reply-To: <fc3lv6$ovb$1@fred.mathworks.com>
Xref: news.mathworks.com comp.soft-sys.matlab:427741



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));