Index Non-Empty Cells in Cell Array

Hi,
I have the following cell array
TEST = [1] [] [] [] []
I want to get the index position of the non-empty cell. I know I can do this using a loop, but is there any single command (like "find") that will do this?
Thank you!
JF

 採用された回答

Honglei Chen
Honglei Chen 2012 年 6 月 28 日
編集済み: Honglei Chen 2012 年 6 月 28 日

33 投票

x = {1,[],[],[]};
find(~cellfun(@isempty,x))

6 件のコメント

John F
John F 2012 年 6 月 28 日
Thanks a lot!
Jan
Jan 2012 年 6 月 28 日
And as usual I mention that "cellfun('isempty', x)" is faster. Although the documentation claims, that this style is kept for backward compatibility only, the string commands are built into the Mex, while the function handle commands require a return to the Matlab interpreter for each cell element.
Honglei Chen
Honglei Chen 2012 年 6 月 28 日
Never thought about that but it surely is good to know, thanks Jan.
Christos Oreinos
Christos Oreinos 2016 年 10 月 6 日
The change in performance is significant for very large cells:
>> x=cell(10000000, 1);
>> x(1980) = {1};
>> tic; find(~cellfun(@isempty, x)); toc
Elapsed time is 5.852150 seconds.
>>
>> tic; find(~cellfun('isempty', x)); toc
Elapsed time is 0.041747 seconds.
Md. Mubarak Hossain
Md. Mubarak Hossain 2017 年 5 月 16 日
a={1 [] [];2 [] 4;5 6 []} find(~cellfun(@isempty,a)) ans =
1
2
3
6
8
Here I'm getting answer in column wise. But How to get row wise answer.
That's means I wanna get
ans=1 4 6 7 8 .
How to get it?
Serge Kogan
Serge Kogan 2020 年 10 月 21 日
Md. Mubarak Hossain, You can get it by transposing the cell array a :
a={1 [] [];2 [] 4;5 6 []};
find(~cellfun(@isempty,a'))

サインインしてコメントする。

その他の回答 (1 件)

Attila
Attila 2017 年 5 月 16 日

1 投票

Dear Mubarak,
This will work : find(~cellfun(@isempty,a'));

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2012 年 6 月 28 日

コメント済み:

2020 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by