search for a code in a cell
1 view (last 30 days)
Show older comments
I want to find whether code [1 1 -1 -1] is present in a cell C in which C{1}=[1 1 1 1],C{2}=[1 1 -1 -1],C{3}=[1 -1 1 -1],C{4}=[1 -1 -1 1]. Also find out what is the location of the code.
0 Comments
Accepted Answer
Evan
on 18 Feb 2015
Edited: Evan
on 18 Feb 2015
key = [1 1 -1 -1]
C{1} = [1 1 1 1];C{2}=[1 1 -1 -1];C{3}=[1 -1 1 -1];C{4}=[1 -1 -1 1]
loc = find(cellfun(@(x)all(x == key),C));
You could also do this with a for loop, checking each cell of C one iteration at a time. cellfun effectively does this.
4 Comments
Stephen23
on 19 Feb 2015
Edited: Stephen23
on 19 Feb 2015
strfind works on numeric arrays, and not on cell arrays containing numeric arrays. Try it with a simple numeric vectors first to see how it works. Although you have already accepted Evan's answer, this might be more what you are wanting:
cellfun(@(c)strfind(c,key), C)
Or if there might be multiple matches / non-matched entries:
cellfun(@(c)strfind(c,key), C, 'UniformOutput',false)
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!