How to vectorize strfind

4 ビュー (過去 30 日間)
Paolo Binetti
Paolo Binetti 2016 年 12 月 22 日
編集済み: Paolo Binetti 2016 年 12 月 23 日
Is it possible to use strfind in a vectorized way? Suppose I want to get find not just one pattern inside a string, but several of them at the same time, so that the output would not be a vector of indexes, but a matrix: is it possible?
Concretely, take a string 'TRSDGHNENJRRDSENTRFDGDGT'. I want to find 'TR', 'DG', and 'EN'. The output of the function would be a matrix 3 x length(string) where line one are zeros and ones at indexes relative to 'TR', line two for 'DG' and line three for 'EN'. Possible?
The purpose is to avoid a for loop which even with pre-allocation is time-consuming. But I actually don't even know if this vectorization I am thinking of would be quicker.
  1 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 23 日
Define quick for you. Exactly how long is your for loop solution taking? I can do around five hundred million iterations of a for loop in less than half a second. How many billions of iterations are you doing in your loop, and how long is it actually taking?

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 12 月 22 日
In R2016b,
S = 'TRSDGHNENJRRDSENTRFDGDGT';
targets = ['TR'; 'DG'; 'EN'];
output = [targets(:,1) == S(1:end-1) & targets(:,2) == S(2:end), false(size(targets,1),1)];
In earlier versions you would need to use bsxfun()
  2 件のコメント
Paolo Binetti
Paolo Binetti 2016 年 12 月 23 日
編集済み: Paolo Binetti 2016 年 12 月 23 日
Thank you but I do not have these functions. On the other hand, I found that I can do it by converting the char array containing all the patterns to be searched into a cell array, and feed this to strfind. But the cell array is too memory-intensive, and the vectorized strfind is too slow, when applied to my problem (not the example).
Walter Roberson
Walter Roberson 2016 年 12 月 23 日
The bsxfun version would be
output = [bsxfun(@eq, targets(:,1), S(1:end-1)) & bsxfun(@eq, targets(:,2), S(2:end)), false(size(targets,1),1)];
If you do not have bsxfun then you must be using a version before R2007a, and if you are then it is important for us to know that so that we do not keep suggesting features you cannot use.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by