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

3 ビュー (過去 30 日間)
David Goldsmith
David Goldsmith 2011 年 7 月 29 日
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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by