COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Searches vector

• Email
• Follow

```Hi everybody

Under I have copied the code for the function searchvec that search through a vector vec to find the value key.
As an example vec=[15 10 7 8 98 8 3 8] and key=8. The function returns [4 6 8]. What I want is that key should be a vector instead of a scalar. As example vec=[15 10 7 8 98 8 3 8] and key=[15 8]. The function should return [1 4 6 8]. How can I change the function file. It must be a function.

function [output]=searchvec(vec,key)

len=length(vec);

%index=[];

i=1;
while i < len %&& vec(i)~=key
i=i+1;

if vec(i)==key
index(i)={i};

end

end

output=cell2mat(index);

Best Regards

Hans
```
 0
Reply Hans 12/30/2009 11:49:03 AM

See related articles to this posting

```"Hans " <jyde_6@msn.com> wrote in message <hhfenf\$j31\$1@fred.mathworks.com>...
> Hi everybody
>
> Under I have copied the code for the function searchvec that search through a vector vec to find the value key.
> As an example vec=[15 10 7 8 98 8 3 8] and key=8. The function returns [4 6 8]. What I want is that key should be a vector instead of a scalar. As example vec=[15 10 7 8 98 8 3 8] and key=[15 8]. The function should return [1 4 6 8]. How can I change the function file. It must be a function.

one of the many solutions

v=[2,3,15,8,8,0,1,15];
k=[8,15];
r=find(ismember(v,k))
%    r = 3 4 5 8

us
```
 0
Reply us 12/30/2009 12:07:03 PM

```"Hans " <jyde_6@msn.com> wrote in message <hhfenf\$j31\$1@fred.mathworks.com>...
> Hi everybody
>
> Under I have copied the code for the function searchvec that search through a vector vec to find the value key.
> As an example vec=[15 10 7 8 98 8 3 8] and key=8. The function returns [4 6 8]. What I want is that key should be a vector instead of a scalar. As example vec=[15 10 7 8 98 8 3 8] and key=[15 8]. The function should return [1 4 6 8]. How can I change the function file. It must be a function.
>

help ismember

HTH,
John
```
 0
Reply John 12/30/2009 12:08:03 PM

```"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hhffr3\$sd2\$1@fred.mathworks.com>...
> "Hans " <jyde_6@msn.com> wrote in message <hhfenf\$j31\$1@fred.mathworks.com>...
> > Hi everybody
> >
> > Under I have copied the code for the function searchvec that search through a vector vec to find the value key.
> > As an example vec=[15 10 7 8 98 8 3 8] and key=8. The function returns [4 6 8]. What I want is that key should be a vector instead of a scalar. As example vec=[15 10 7 8 98 8 3 8] and key=[15 8]. The function should return [1 4 6 8]. How can I change the function file. It must be a function.
> >
>
> help ismember
>
> HTH,
> John

Dear John

I have found a solution.

function [output]=smartseqsearch(vec,key)

len=length(vec);

%index=[];

i=1;

while i < len %&& vec(i)~=key
i=i+1;

for j=1:length(key)
if vec(i)==key(j)
index(i)={i};

end
end
end

output=cell2mat(index);

I will look at the function ismember to see if I can optimise my code.

Best Regards

Hans
```
 0
Reply Hans 12/30/2009 12:15:05 PM

```"us " <us@neurol.unizh.ch> wrote in message <hhffp7\$ot9\$1@fred.mathworks.com>...
> "Hans " <jyde_6@msn.com> wrote in message <hhfenf\$j31\$1@fred.mathworks.com>...
> > Hi everybody
> >
> > Under I have copied the code for the function searchvec that search through a vector vec to find the value key.
> > As an example vec=[15 10 7 8 98 8 3 8] and key=8. The function returns [4 6 8]. What I want is that key should be a vector instead of a scalar. As example vec=[15 10 7 8 98 8 3 8] and key=[15 8]. The function should return [1 4 6 8]. How can I change the function file. It must be a function.
>
> one of the many solutions
>
>      v=[2,3,15,8,8,0,1,15];
>      k=[8,15];
>      r=find(ismember(v,k))
> %    r = 3 4 5 8
>
> us

Thank you very much US. I will try the ismember and use the tic toc to see what is fastest.

Best Regards

Hans
```
 0
Reply Hans 12/30/2009 12:19:03 PM

```"Hans " <jyde_6@msn.com> wrote in message <hhfgfm\$93o\$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hhffp7\$ot9\$1@fred.mathworks.com>...
> > "Hans " <jyde_6@msn.com> wrote in message <hhfenf\$j31\$1@fred.mathworks.com>...
> > > Hi everybody
> > >
> > > Under I have copied the code for the function searchvec that search through a vector vec to find the value key.
> > > As an example vec=[15 10 7 8 98 8 3 8] and key=8. The function returns [4 6 8]. What I want is that key should be a vector instead of a scalar. As example vec=[15 10 7 8 98 8 3 8] and key=[15 8]. The function should return [1 4 6 8]. How can I change the function file. It must be a function.
> >
> > one of the many solutions
> >
> >      v=[2,3,15,8,8,0,1,15];
> >      k=[8,15];
> >      r=find(ismember(v,k))
> > %    r = 3 4 5 8
> >
> > us
>
>
> Thank you very much US. I will try the ismember and use the tic toc to see what is fastest.
>
> Best Regards
>
> Hans

Dear John and US.

What if it was a cellarray it has to search through and find some values?How would the code looklike?

Best Regards

Hans
```
 0
Reply Hans 12/30/2009 12:27:03 PM

```"Hans "
> Dear John and US.
>
> What if it was a cellarray it has to search through and find some values?How would the code looklike?
>
> Best Regards
>
> Hans

one of the solutions

v=num2cell([2,3,15,8,8,0,1,15]);     % a CELL
k=[8,15];
r=find(ismember([v{:}],k))
%    r = same result as above...

us
```
 0
Reply us 12/30/2009 1:38:02 PM

6 Replies
203 Views

Similar Articles

12/20/2013 4:25:04 PM
page loaded in 4583 ms. (0)

 Reply:

Similar Artilces:

Searching a vector with functionals
Hi folks, I'm trying to implement a functor find_if, which takes a predicate, and searches a container (for example, vector) and returns (a reference to) the first element for which the predicate function returns true. I can't get it to work. Maybe someone can give me a hint? From my understanding, the call and the template-generated functions do match, typewise. The problem appears to be that main()::f does not satisfy std::unary_function<element_t&, bool>& but I don't understand why. gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) gives me the following e...

Vectorizing 3d search
Hello, I am trying to speed up a basic algorithm for searching through a large amount of spatial data. The way I currently have it written is taking too long to execute. Can someone tell me how to vectorize the following 2-D search window? Thank you. >>>>>>>>>>>>>> %X(i), Y(i), Z(i) = 3d points. n=.10; t=10; g=1000; i = 5000; e=0; f=0; while (i~=0) a = Z(i)-n; b = Z(i)+n; c = Y(i)-t; d = Y(i)+t; inds = find(CloudZ>a & CloudZ<b & CloudY>c & CloudY<d); if (length(inds)>g...

searching in set vs. vector
I have found an article with 50 STL tips.. I am wondering about tip #23: -- snip -- 23) Consider replacing associative containers with sorted vectors. Associative containers are implemented as balanced binary search trees, good for a mix of insertions, erasures and lookups. If insertions are mostly separate from lookups, you can use vectors which would take less memory, sort it and use lookups at a cost of log n. -- snip -- I have made a test on GNU/Linux using g++ 4.0 comparing searches in std::vector<int> and std::set<int> and it seems searches in sorted vector are faster only...

adding search capability to a Vector
Hello, I am trying to add a simple search behaviour to a STL Vector like the following: //---- declaration --- typedef struct { std::string name; std::string favouriteFood; } TBuddy; typedef TBuddy* PBuddy; class TBuddies: public std::vector<PBuddy> { public: std::string favouriteFoodOf(const std::string& name) const; }; //---- implementation --- std::string TBuddies::findValue(const std::string& name) const { for(TBuddies::iterator i= this->begin(); i!= this->end(); ++i) { PBuddy b = *i; if(b->name ==s) return b->favouriteFood; } return ""; ...

Algorithm for searching across vectors
Hi, I am looking for an efficient algorithm to search for k-median across disjointed vectors. For example, I have 10 different size arrays of floats (size varies from 20k to 100k elements) from which I would like to get 1000th element of the combined set (total set is 500k elements). Currently I am extracting 1000 elements from each array and doing merge-sort. Since this operation is performed atleast 100k times (i.e. a set of 100k * 500k elements), the constant factor of shipping elements across a different machine is very high. I am wondering if there is a better approach to limit the am...

Searching a vector for a specific value
Hi. Could anyone tell me how one can search a vector for a specific value? std::vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); How to check whether the number three is in the vector? I tried "find" but it doesn't appear to be available to "vector". Any help appreciated. Regards, -- Siegen ---------------------- --------------------- Please see http://www.cppreference.com/cppalgorithm/find.html. It may give you some hint. Siegen wrote: > Hi. > > Could anyone tell me how one can sear...

Vectorized search in nested struct array
I have a database that has the following structure: gear(:).data.field_1 gear(:).data.field_2 gear(:).data.field_n gear(:).data.G(:).name gear(:).data.G(:).phield_1 gear(:).data.G(:).phield_2 gear(:).data.G(:).phield_m In order to access the data in the fields of struct array G I have thought of a function that returns the indices (i,j) if I search for the contents of G(:).name (a string, using strcmp). Something like [i,j]=db_find(gear,str) where str is the string to look for in gear(:).data.G(:).name The goal is to have an efficient code (e.g. avoiding for-loops) as length(gear) and lengt...

Searching a vector for a specific value #2
Hi. Could anyone tell me how one can search a vector for a specific value? std::vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); How to check whether the number three is in the vector? I tried "find" but it doesn't appear to be available to "vector". Any help appreciated. Regards, -- Siegen ---------------------- --------------------- Siegen wrote: > Could anyone tell me how one can search a vector for a specific value? > > std::vector<int> vec; > > vec.push_back(1); >...

Efficient searching of one vector with another
Hello, I would like to find the most efficient way to search a vector for the first value greater than or equal to values in a second vector. I have working code, but it is slow because of a for loop and the find function call. In my particular case, the search space is defined on a logarithmic scale, but I see no reason why the principle can't be adapted to any sort of search space. Here's a simplified version of the code I have that works. However, if search_vals gets above 1000, the snipit of code will take several seconds to execute on my laptop. Since I eventually will nee...

Question on find on a vector
I assume that find on a vector is a linear search a bit like iterating through the items in vector until first instance found? If so, then I suppose it is slightly more convenient than writing code to iterate vector. If more than the odd search required obviously would be better to sort vector and use lower_bound I guess. On 3/22/2011 7:52 AM, Angus wrote: > I assume that find on a vector is a linear search a bit like iterating > through the items in vector until first instance found? > > If so, then I suppose it is slightly more convenient than writing code > ...