from
collapse.m
by Gerald Dalley
Removes degenerate portions of a cell array tree.
|
| collapse(c)
|
function c = collapse(c)
%c = collapse(c)
% Takes a recursive cell array as a tree and collapses any branches
% having a fanout of 1. Any elements with fanout of 0 are removed.
%
%Examples:
% collapse({1 2 3}) --> {1 2 3}
% collapse({{1} {2} {{3}}}) --> {1 2 3}
% collapse({1 {2 3}}) --> {1 {2 3}}
% collapse({1 {{} 2 {{}} 3}}) --> {1 {2 3}}
% collapse({{{}}}) --> {}
%
% [matches] = regexp({'abc', 'def', 'fghi'}, '[^f]*f+[^f]*', 'match');
% collapse(matches) --> {'def' 'fghi'}
%
%
%by Gerald Dalley
if (iscell(c))
toRemove = zeros(1,length(c));
for i=1:length(c)
newEntry = collapse(c{i});
if iscell(newEntry)
switch length(newEntry)
case 0,
toRemove(i) = 1;
case 1,
c{i} = newEntry{1};
otherwise,
c{i} = newEntry;
end
end
end
c = {c{~toRemove}};
end
|
|
Contact us at files@mathworks.com