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

2 次查看(过去 30 天)
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

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-7-29
a={[], {3}, [], [], eye(100)};
b=find(~cellfun('isempty',a))

更多回答(1 个)

David Goldsmith
David Goldsmith 2011-7-29
Jackpot, thanks!!!
Awesome, I wasn't aware of cellfun (obviously); thanks again!

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by