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?

2
  • What you have there is not a vector of strings. Oh, and instead of cellfun(@isempty, ...) use cellfun('isempty', ...). It should run much faster. Commented Jul 16, 2013 at 8:19
  • Thank you but it is still very slow. Commented Jul 16, 2013 at 10:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.