How do I see the index values of strfind ?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to use strfind to locate values in a cell array and then compare the adjacent cells to a string that I have in order to see if the string matches the array. Ex.
A={'a' 'c' 'a' 't' 'e' 'f' 'g'}
B=strfind(A,'a')
2 Kommentare
Guillaume
am 25 Nov. 2015
A=['a' 'c' 'a' 't' 'e' 'f' 'g']
is not a cell array. It is an array of character, basically a string and the above line is the same as
A = 'acatefg'
So, have you got a cell array of single characters
A = {'a' 'c' 'a' 't' 'e' 'f' 'g'}
a string as you've shown in your example, or something else?
Antworten (2)
Guillaume
am 25 Nov. 2015
If you do have a cell array, unlike your example (see comment to your question), and you're trying to match a string in the cell array, then it is not strfind you need but strcmp:
A = {'a' 'c' 'a' 't' 'e' 'f' 'g'}
find(strcmp(A, 'a'))
0 Kommentare
Star Strider
am 25 Nov. 2015
You can’t find 'b' in your ‘A’ array because there is no 'b' in ‘A’.
Please describe in a bit more detail what you want to do.
3 Kommentare
Star Strider
am 25 Nov. 2015
This is not robust, but will allow you to find the indices of the two (or fewer) characters next to the character-of-interest. If there are fewer than two, it includes the index of your variable-of-interest, but that is likely unavoidable:
A=['a' 'c' 'a' 't' 'e' 'f' 'g']
B=strfind(A,'a')
Bnext = [max(1,B(1)-1), min(B(1)+1,length(A)); max(B(1),B(2)-1), min(B(2)+1,length(A))];
Bnext =
1 2
2 4
You can build on it to make it more robust to fit your needs.
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!