MATLAB Answers

JoseLuis
0

Get the index for the search of multiple strings within an array of strings

Asked by JoseLuis
on 12 Jul 2012
Latest activity Commented on by Dhruv Ghulati on 21 Dec 2015
Accepted Answer by Jan
Hi,
I have a (1x128) cell array like this: "list" = 'S01R' 'S02R' 'S03R'... etc And another short (1x16) cell array with the same type of characters, eg, "bad" = 'S01R' 'S02R' 'S03R'... etc
I want to know the index of my "list" where I may found the items listed in "bad", I'v tried regexp but this function only allow me to find single strings in my list, Also, I don't know how to get the index in a orderer list instead of a cell array like this c=[] [] [] [1] [] [] [] [] []
Thanks for any idea,
JL

  1 Comment

Please post examples in valid Matlab code, such that they can be used directly in an answer by copy&paste.

Sign in to comment.

4 Answers

Answer by Jan
on 12 Jul 2012
 Accepted Answer

See FEX: CStrAinBP: Reply the indices of common strings.

  0 Comments

Sign in to comment.


Answer by Ryan
on 12 Jul 2012
Edited by Ryan
on 12 Jul 2012

Is this what you were looking for?
list = {'bad','worse','good','okay'};
badlist = {'bad','worse'};
idx = ismember(list,badlist);
This returns:
idx = 1 1 0 0;

  1 Comment

This works for exact text search, but not for if a string contains a particular set of characters. I find strfind() a better approach if you have that particular use case.

Sign in to comment.


Answer by F.
on 12 Jul 2012

did you try "intersect" ???
[C,ia,ib] = intersect(A,B) example

  0 Comments

Sign in to comment.


Answer by JoseLuis
on 12 Jul 2012

Thanks to all for your answers, I found something that works, http://www.mathworks.com/matlabcentral/newsreader/view_thread/310652#846320
CStrAinBP it does make it too,
JL

  0 Comments

Sign in to comment.