help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applying function to vector by index


From: Paul Kienzle
Subject: Re: Applying function to vector by index
Date: Mon, 9 Dec 2002 11:56:46 -0500
User-agent: Mutt/1.2.5.1i

Iago,

Sparse matrices are a likely candidate here.  Split your data with
one column per bin:

        n = length(d);
        S = sparse(1:n,idx,d);

Then the stats are:

        count = spsum(sparse(1:n,idx,1));
        mean = spsum(S) ./ count;
        var = spsum( (S - sparse(1:n,idx,mean(idx))).^2 ) ./ (count-1);

Not as good as a generic split/apply operations I agree, but even
with split/apply performance wouldn't be much better than a loop since
you would still be interpreting var(x) for each bin separately.

Paul Kienzle
address@hidden

On Mon, Dec 09, 2002 at 04:22:17PM -0000, Iago Mosqueira wrote:
> Hi,
> 
> I have a long vector classified in ten bins of different length according to
> another vector. I want to estimate the variance, or any other function, of
> the values from the first vector for each of the categories described by the
> second, without using a loop. Is there any obvious way of doing this I am
> overlooking? In R one could do
> 
>     sapply(split(d,idx),var)
> 
> where d is my data vector and idx the index vector.
> 
> Many thanks,
> 
> 
> iago
> 
> 
> 
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------
> 



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]