Search entire multi level cell array for matching string
Show older comments
I am trying to search a cell of cell arrays for a matching string without using a for loop due to the inefficiency and speed. My first idea was to use an anonymous function and strip out the top layer iteratively of the cell array in the form of
cellfun(@(cell) cell{1}, MyCellofCellArray, 'UniformOutput', false);
However, let's say for example I have an uneven cell of cell array where
MyCellArray{1} = 1x1 cell,
MyCellArray{2} = 1x2 cell,
MyCellArray{3} = 1x3 cell.
If you try to use the aforementioned cellfun, you get an error "Index exceeds matrix dimensions" because MyCellArray{1} only contains a 1x1 cell. Anyone have any ideas or suggestions?
Thanks,
JWelch
3 Comments
Cedric
on 6 Aug 2015
Could you provide an example, and indicate what you are looking for? I.e. do you need to extract chunks of matched strings, IDs of external and internal cell arrays, ..?
Josh Welch
on 6 Aug 2015
Accepted Answer
More Answers (1)
If you have 2015a or above, this can be an occasion for playing with the new REPELEM function:
linId = strcmpi( [MyCellArray{:}], 'str32' ) ;
lookup = repelem( 1:numel(MyCellArray), cellfun( @numel, MyCellArray )) ;
id = [lookup(linId), sum( lookup(1:find(linId) == lookup(linId)) )] ;
This produces
id =
3 2
2 Comments
Josh Welch
on 6 Aug 2015
My pleasure! I discovered REPELEM last week actually, so I'm glad to have had this occasion for playing a bit with it. Just for those who don't have 2015a or above, here is the output of intermediary steps:
linId =
0 0 0 0 1 0
lookup =
1 2 2 3 3 3
Categories
Find more on Variables 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!