help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "find" equivalent for strings?


From: Søren Hauberg
Subject: Re: "find" equivalent for strings?
Date: Tue, 13 Sep 2005 21:44:08 +0200
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050912)

Hi,
If you have Octave-forge you can do something like this

function out = is_bob(name)
        out = strcmp(name, "bob");
endfunction

cellfun("is_bob", x);

which gives you:

ans =

{
  [1,1] = 1
  [1,2] = 0
  [1,3] = 0
  [1,4] = 1
  [1,5] = 1
}

Hope that's what you look for

/Søren

Mike Miller wrote:
Simple example input file:

bob
joe
sally
bob
bob

reading in the data:

x=textread("file.txt","%s");

x =

{
  [1,1] = bob
  [1,2] = joe
  [1,3] = sally
  [1,4] = bob
  [1,5] = bob
}

Now I want to determine which elements of x consist of "bob" (so I want the vector [1,4,5] returned), but I'd rather not use a loop. Is there any way to do this without doing strcmp on every element of the cell?

Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------




-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]