From: Hans on
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
From: us on
"Hans " <jyde_6(a)msn.com> wrote in message <hhfenf$j31$1(a)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
From: John D'Errico on
"Hans " <jyde_6(a)msn.com> wrote in message <hhfenf$j31$1(a)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
From: Hans on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <hhffr3$sd2$1(a)fred.mathworks.com>...
> "Hans " <jyde_6(a)msn.com> wrote in message <hhfenf$j31$1(a)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
From: Hans on
"us " <us(a)neurol.unizh.ch> wrote in message <hhffp7$ot9$1(a)fred.mathworks.com>...
> "Hans " <jyde_6(a)msn.com> wrote in message <hhfenf$j31$1(a)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
 |  Next  |  Last
Pages: 1 2
Prev: diameter
Next: contrability in state space system