0

I would like to search a vector of strings in two columns of a cell array of strings A (300.000 x 7).

 string=[53716;59428;58221;679854].

Here below is the code:

y=arrayfun(@(x)~cellfun(@isempty,regexp(A(:,3:4),string(x))),1:numel(string),'uni',false);

y=cat(1,y{:});

This question is similar to How to search for a string in cell array in MATLAB? and this one Searching cell array with regex

However this solution takes hours. Does anyone know a more efficient way to perform the same operation?

Community
  • 1
  • 1
seli
  • 67
  • 9
  • What you have there is not a vector of strings. Oh, and instead of `cellfun(@isempty, ...)` use `cellfun('isempty', ...)`. It should run much faster. – Eitan T Jul 16 '13 at 08:19
  • Thank you but it is still very slow. – seli Jul 16 '13 at 10:10

1 Answers1

2

You are comparing strings to doubles ("string" is a double array); is that what you want to do? If not, you could use string_chars=arrayfun(@(x) sprintf('%d',string(x)),1:length(string),'uni',false);

To avoid regexp, you could use strcmp:

result = zeros(size(A(:,3:4)));
for v=1:length(string_chars)
    result=result+v.*strcmp(A(:,3:4),string_chars(x)); % Should string be a cell here, btw?
end

I don't know if adding the results as I am doing here suits your use, but you can adapt as needed. This will set elements of result to correspond to each element of string_chars.

Hugh Nolan
  • 2,508
  • 13
  • 15
  • +1. If more speed is the goal, avoid Matlab's pathetically slow regex implementation especially for simple comparisons like this. And of course never be afraid of `for` loops. – horchler Jul 16 '13 at 14:22