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 |
|
"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
170 Views
(page loaded in 0.034 seconds)
|