Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave - return the position of the first occurrence of a string in a cell array

Is there a function in Octave that returns the position of the first occurrence of a string in a cell array?

I found findstr but this returns a vector, which I do not want. I want what index does but it only works for strings.

If there is no such function, are there any tips on how to go about it?

like image 402
CH123 Avatar asked Apr 28 '16 14:04

CH123


People also ask

What is character array in octave escape sequence?

It can be of any size. Character array is used to represent a string in Octave Escape Sequence : Some double-quoted (“) string containing backslash (‘\’), change the meaning of that string constant, these types of strings are special and are used for special purposes.

What is a string in octave?

In Octave GNU, A string is basically the collection of characters enclosed between double quotes (“) or single quotes (‘). In Octave, there is no limit for the length of the string. i.e. It can be of any size. Character array is used to represent a string in Octave

How do you find the last occurrence of a string?

Return the position of the last occurrence of the character string t in the character string s, or 0 if no occurrence is found. s may also be a string array or cell array of strings. The rindex function is equivalent to index with direction set to "last" . See also: find, index .

How to find the first occurrence of a value in a range?

COUNTIF is an easy and useful function to find the first occurrence of a value in a range. It takes single or multiple criteria and ranges based on singular or plural function usage. We will see each of them below. The easiest formula to find the first occurrence of a value in a range is using the COUNTIF function.


1 Answers

As findstr is being deprecated, a combination of find and strcmpi may prove useful. strcmpi compares strings by ignoring the case of the letters which may be useful for your purposes. If this is not what you want, use the function without the trailing i, so strcmp. The input into strcmpi or strcmp are the string to search for str and for your case the additional input parameter is a cell array A of strings to search in. The output of strcmpi or strcmp will give you a vector of logical values where each location k tells you whether the string k in the cell array A matched with str. You would then use find to find all locations of where the string matched, but you can further restrain it by specifying the maximum number of locations n as well as where to constrain your search - specifically if you want to look at the first or last n locations where the string matched.

If the desired string is in str and your cell array is stored in A, simply do:

index = find(strcmpi(str, A)), 1, 'first');

To reiterate, find will find all locations where the string matched, while the second and third parameters tell you to only return the first index of the result. Specifically, this will return the first occurrence of the desired searched string, or the empty array if it can't be found.

Example Run

octave:8> A = {'hello', 'hello', 'how', 'how', 'are', 'you'};
octave:9> str = 'hello';
octave:10> index = find(strcmpi(str, A), 1, 'first')
index =  1
octave:11> str = 'goodbye';
octave:12> index = find(strcmpi(str, A), 1, 'first')
index = [](1x0)
like image 149
rayryeng Avatar answered Nov 15 '22 05:11

rayryeng