Eri*_*ski 32

Octave搜索单元格数组:

cellidx按八度音程折旧,它仍然运行,但他们说使用ismember,如下所示:

%linear time string index search.
a = ["hello"; "unsorted"; "world"; "moobar"]
b = cellstr(a)
%b =
%{
%  [1,1] = hello
%  [2,1] = unsorted
%  [3,1] = world
%  [4,1] = moobar
%}
find(ismember(b, 'world'))   %returns 3
Run Code Online (Sandbox Code Playgroud)

'world'在索引槽2中找到.这是一个昂贵的线性时间O(n)操作,因为它必须线性迭代所有元素,无论它是否被找到.

您应尝试以不同方式对问题进行建模,以尝试获得恒定时间O(1)logarathmic时间O(log n)解决方案.


Pet*_*nko 11

是的,请查看:http://www.obihiro.ac.jp/~suzukim/masuda/octave/html3/octave_36.html#SEC75

a = ["hello"; "world"];
c = cellstr (a)
     ? c =
         {
           [1,1] = hello
           [2,1] = world
         }
>>> cellidx(c, 'hello')
ans =  1

>>> cellidx(c, 'world')
ans =  2
Run Code Online (Sandbox Code Playgroud)

  • cellidx在Octave 4.0.2中不起作用 (3认同)