Indexing way to retrieve indices of nonempty elements of a cell array?

9 visualizaciones (últimos 30 días)
Is there an indexing way (I couldn't figure one out) to do what this little function does:
function nonempty = findNonEmpty(cellarray)
% Find indices of non-empty array elements of cellarray
% Example:
% findNonEmpty({[], {3}, [], [], eye(100)}) returns [2 5]
nonempty = [];
for i=1:length(cellarray)
if ~isempty(cellarray{i})
nonempty(end+1) = i;
end
end

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 29 de Jul. de 2011
a={[], {3}, [], [], eye(100)};
b=find(~cellfun('isempty',a))

Más respuestas (1)

David Goldsmith
David Goldsmith el 29 de Jul. de 2011
Jackpot, thanks!!!
Awesome, I wasn't aware of cellfun (obviously); thanks again!

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by