help-octave
[Top][All Lists]

## Re: Applying function to vector by index

 From: Andy Adler Subject: Re: Applying function to vector by index Date: Wed, 11 Dec 2002 12:41:33 -0500 (EST)

```I have committed the patch for sparse.^scalar
to octave-forge/main/sparse
Octave now behaves like Matlab for this one.

Note: I have not applied John's STL patch to
sparse. I am wondering if it's worth writing some
CPP magic so that it works with 2.1.40, or to wait
until 2.1.41 comes out.

Could anyone comment on whether 2.1.41 is soon?

Andy
--
Assistant Professor, School of Information Technology and Engineering,
University of Ottawa, Tel:1(613)562-5800 X 2345, Email:address@hidden

On Mon, 9 Dec 2002, Andy Adler wrote:

> I've got most of a patch in place, but there's all
> sorts of weird side cases.
>
> For example, what should sparse(eye(2)).^(1+1i) give?
>
> Matlab gives:
> >> full(sparse(eye(2)).^(1+1i))
>
> ans =
>
>      1     0
>      0     1
>
> >> full(full(eye(2)).^(1+1i))
>
> ans =
>
>    1.0000             NaN +    NaNi
>       NaN +    NaNi   1.0000
>
>
> That's because a^(b+ci) = e^(ln(a))*(b+ci)
> and ln(a) is NaN
>
> Should we follow this behaviour?
>
>
> Andy
> --
> Assistant Professor, School of Information Technology and Engineering,
> University of Ottawa, Tel:1(613)562-5800 X 2345, Email:address@hidden
>
>
> On Mon, 9 Dec 2002, Paul Kienzle wrote:
>
> > That's because S.^2 implicitly expands S to a full matrix 8-(
> > Anyone care to patch it?
> >
> > Meanwhile, how about the following:
> >
> > function [count, mean, diff] = spstats(v,j)
> >
> >     i = [1:length(v)];
> >     S = sparse(i,j,v);
> >     count = spsum(sparse(i,j,1));
> >     mean = spsum(S) ./ count;
> >     diff = S - sparse(i,j,mean(j));
> >     var = spsum ( diff .* diff ) ./ (count - 1);
> >
> > end
> >
> > Paul Kienzle
> >
> > On Mon, Dec 09, 2002 at 05:01:47PM -0000, Iago Mosqueira wrote:
> > > Thanks, it works fine. I was doing just this for estimating the mean, but
> > > I
> > > was somehow getting into memory problems (a 65000x1 vector, turning into a
> > > sparse matrix with 15 datapoints). It's fine now. Yes, some general way
> > > 'sapply' style would be nice...
> > >
> > > Thanks,
> > >
> > >
> > > iago
> > >
> > >
> > > ----- Original Message -----
> > > From: "Paul Kienzle" <address@hidden>
> > > To: "Iago Mosqueira" <address@hidden>
> > > Cc: "octave-help" <address@hidden>
> > > Sent: 09 December 2002 16:56
> > > Subject: Re: Applying function to vector by index
> > >
> > >
> > > > 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
> > > > -------------------------------------------------------------
> > > >
> > >
> > >
> > >
> > > -------------------------------------------------------------
> > > 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
> > -------------------------------------------------------------
> >
> >
>
>

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