【问题标题】:Find Strings(keywords) in a large MATLAB table在大型 MATLAB 表中查找字符串(关键字)
【发布时间】:2016-09-14 02:16:51
【问题描述】:

我有大型 MATLAB 表 (2.000.000 x 30),第一列中有时间戳,第二列中有参数组关键字。第三列是参数名称,下一列是参数值。其次是更多的参数和值对。

示例数据:

2012-08-28 13:37:46:573, m_IVS_AU_SystemManager_FlashLast, flashLast, 9, ...    
2012-08-28 13:37:46:573,m_IVS_AU_SystemManager_CPULast,cPULast,51, ...    
2012-08-28 13:37:46:573,m_IVS_AU_SystemManager_NetworkLast,networkLast,0, ...    
2012-08-28 13:37:48:558,m_F121_KantenKB,kantenKB,0, KantenKU, 1, ...    
2012-08-28 13:37:48:589,m_F121_HmiVollstaendig,hmiVollstaendig,TRUE:0, ...    
2012-08-28 13:37:49:042,m_F22_IVS_IsecTrkServiceState,isecTrkServiceState,3, ...    
2012-08-28 13:37:49:120,m_F121_HmiUpdate,hmiUpdate_PresentationsId,0, ...

我的任务

我需要从此 MATLAB 表中提取与关键字列表匹配的条目(例如:m_IVS_AU_SystemManager_FlashLast),并将时间戳和参数值保存到新的 MATLAB 表中。

有没有(快速)的方法来做类似的事情

rows = MyTable.parameterKeyword == 'm_IVS_AU_SystemManager_FlashLast'

就像 MATLAB 中的数字一样,如果在该行中找到关键字,则返回包含逻辑真 (1) 的逻辑数组,否则为零?

链接

【问题讨论】:

  • 您是在搜索表中的特定变量(列)还是整个事物?
  • 我总是在第二列搜索某些关键字。

标签: string performance matlab


【解决方案1】:

您可以为此使用ismember,这将返回一个logical 数组。

例如:

% Dummy table
B = table(['M';'F';'F';'F'],[47;31;35;23],[68;64;62;58],...
'VariableNames',{'Gender' 'Age' 'Height'},...
'RowNames',{'Joe' 'Meg' 'Beth' 'Amy'});

mask = ismember(B.Gender, 'F');
whos mask

返回:

mask =

     0
     1
     1
     1

  Name      Size            Bytes  Class      Attributes

  mask      4x1                 4  logical   

如下所述,如果查询仅针对一个字符串,strcmp 或 strcmpi 可用于略微提高速度。

我在下面附上了简单的时间安排:

function testcode(nentries)
A = table(repmat({'hi'}, nentries, 1), 'VariableNames', {'Thing1'});

t1 = timeit(@()ismember(A.Thing1, 'hi'));
t2 = timeit(@()strcmp(A.Thing1, 'hi'));
t3 = timeit(@()strcmpi(A.Thing1, 'hi'));

tmp = ver;
fprintf(['Timing for %d entries %s:\n' ...
         '    ismember: %.5f\n' ...
         '    strcmp:   %.5f\n' ...
         '    strcmpi:  %.5f\n\n'], nentries, tmp.Release, t1, t2, t3);
end

对于testcode(5e6),我的机器上的时间如下:

Timing for 5000000 entries (R2016a):
    ismember: 0.09246
    strcmp:   0.06575
    strcmpi:  0.07284

【讨论】:

  • strcmp 也可以在您只查找一个字符串时使用,好处是您也可以使用strcmpi 进行不区分大小写的匹配。
  • @SomeGuy 是的,我已经添加了比较
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
相关资源
最近更新 更多