On 3/9/2011 4:50 PM, Donald wrote:
> Suppose I have a vector, nVec = [2 7 5 4 2 4] and I want to count the
> number of occurences in this vector, so that I end up with yVec = [2 4 5
> 7]; numVec = [2 2 1 1]
>
> Is there a fast way to do this, besides using unique or hist?
...
I'd think unique() and hist() would be as fast as about
anything--certainly sort()ing isn't fastest thing in world.

W/ your example I'd just do what you say you don't want until I had
actual timing to prove to me it was a real issue that had to deal with
or else...

dpb <none@non.net> wrote in message <il91hp$8ad$1@news.eternal-september.org>...
> On 3/9/2011 4:50 PM, Donald wrote:
> > Suppose I have a vector, nVec = [2 7 5 4 2 4] and I want to count the
> > number of occurences in this vector, so that I end up with yVec = [2 4 5
> > 7]; numVec = [2 2 1 1]
> >
> > Is there a fast way to do this, besides using unique or hist?
> ...
> I'd think unique() and hist() would be as fast as about
> anything--certainly sort()ing isn't fastest thing in world.

unique has a sort engine behind. hist is slower than accumarray. The fastest thing is sorting once.

I haven't done a comprehensive test, but which method is faster depends on the input data vector. If the vector has a small number of elements, but those elements are widely dispersed, the sort method will be faster than then the accumarray method because accumarray will generate a large vector upon which a find will have to be done.

"Donald " <gt4715b@yahoo.com> wrote in message <ilbejl$hqe$1@fred.mathworks.com>...
> I haven't done a comprehensive test, but which method is faster depends on the input data vector. If the vector has a small number of elements, but those elements are widely dispersed, the sort method will be faster than then the accumarray method because accumarray will generate a large vector upon which a find will have to be done.
>
> For example, if nVec = [-500 530 600], accumarray will generate an 1100 element array, whereas sort just sorts the 3 values.

No, accumarray will also generate 3-element array. See my implementation.