Stuart’s MATLAB Videos

Watch and Learn

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Searching for permutations of a string in a list in MATLAB 4

Posted by Doug Hull,

In this video a colleague was looking in a large list to see if each element was a permutation of any element of a shorter list. I name these lists Haystack and Needle so it is easy to remember which I am looking for and which I am looking through. Because we are looking for any permutation of a three character string, it might seem like we would have to search three times for each element in the needle list. It turns out, if you sort the elements of each list, the permutations are no longer an issue. The function ismember is also useful in making this string searching in MATLAB go very quickly.

4 CommentsOldest to Newest

Brad Stiritz replied on : 1 of 4
Hi Doug, I had some trouble with your code in R2013b. As you can see below, I've isolated the error to where the alphabet integer-equivalent vector is generated. What do you think is going on? Thanks, Brad >> needleBig = 500; haystackBig = 125000; >> randi([double('a') double ('z')], needleBig, 3); Error using double Not enough input arguments. % Test elements of the command individually.. >> double('a') ans = 97 >> double ('z') ans = 122 >> [double('a') double ('z')] Error using double Not enough input arguments. % Is there a problem generating the vector manually? The following statement executes OK, so it's not clear what the issue is? >> [97 122]; p.s. since you asked for feedback, IMHO your window-in-window concept would convey a nicer impression with even lighting. (It's a little gloomy-looking at present)
Doug replied on : 2 of 4
Get rid of the space after double:
>>  [double('a') double ('z')]
Error using double
Not enough input arguments.
 
>> [double('a') double('z')]

ans =

    97   122