Blitz logo

Blitz Support :

From: Julian Cummings (cummings_at_[hidden])
Date: 2005-06-01 16:05:10


Hello,

Unfortunately, there is no pre-existing find() function for either blitz
Array or Vector. You could probably whip this up by borrowing from the
existing count() function, which counts the number of elements that satisfy
some condition. Instead of just counting the elements, you would place the
indices into a container and then return the container at the end.

For the blitz Vector class, the count() code is in <blitz/veccount.cc>. You
could copy this code, rename the function to find(), and modify it to return
a Vector<int> containing the indices that evaluate to "true". You should
initially construct this Vector<int> to have the same length as the incoming
Vector expression, and then use the resizeAndPreserve() method to shrink the
outgoing Vector<int> to the proper size. So something like this:

template <typename P_expr>
inline Vector<int> _bz_vec_find(P_expr vector)
{
    int length = vector._bz_suggestLength();
    Vector<int> indices(length);
    int count = 0;

    if (vector._bz_hasFastAccess())
    {
        for (int i=0; i < length; ++i)
            if (vector._bz_fastAccess(i))
                indices(count++) = i;
    }
    else {
        for (int i=0; i < length; ++i)
            if (vector[i])
                indices(count++) = i;
    }

    indices.resizeAndPreserve(count);
    return indices;
}

The blitz Array reduction code is a bit more elaborate, but you could add a
ReduceFind type in <blitz/reduce.h> that would return an
Array<TinyVector<int,N_rank>,1> containing the indices of the Array elements
that satisfy the condition. Then you would add a definition for find() as a
full Array reduction function in <blitz/array/reduce.h> using the ReduceFind
object. I could help you work up some code along these lines if you are
interested. The original Array reduction functions were based upon the
offerings of Fortran 90. That is why there is no find() function provided
currently, but it seems like a useful addition. Please feel free to submit
a proposed patch/extension to blitz if you get something that works to your
liking.

Regards, Julian C.

Dr. Julian C. Cummings
Staff Scientist, CACR/Caltech
(626) 395-2543
cummings_at_[hidden]
 

> -----Original Message-----
> From: blitz-support-bounces_at_[hidden]
> [mailto:blitz-support-bounces_at_[hidden]] On Behalf Of
> Shubhankar Ray
> Sent: Tuesday, May 31, 2005 6:27 PM
> To: blitz-support_at_[hidden]
> Subject: [Blitz-support] analogue of matlab find()
>
> Hello,
>
> I recently started using blitz after coming to know of its
> Matlab like capabilities. I want to know if there is an
> analogue of the Matlab find(). Example, X=[0 1 0 1];
> find(X==1) returns [2 4].
>
> In blitz I see that where() can be used to work on specific
> indices satisfying some expression, but is there a quick way
> to return just the indices as an Array<> or vector<>.
>
> thanks
> Shubhankar Ray
>
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>
>