How to determine which indices in a row cell array contains the value 1, even when there are null vectors [ ] in the cell array?

Hello,
If I have a row cell array,
C = {[], [], [], [], 1, 1, 1, 1, 1, 1, 4, 4, 5, 5, 5, [], [], []}
and I would like to know where in the cell array (i.e. which column) the 1 values are located. How do I grab the indices of where 1 is located because when I use the following,
d = find([b{:}] == 1)
I get the following output.
d = [1, 2, 3, 4, 5, 6]
which is not what I want because it ignores the null vector, []...Ideally, I would like the following output, or something like that.
d = [5, 6, 7, 8, 9, 10]
I'm using MATLAB 2015a btw. Any help or advice would be appreciated. Thanks.

 採用された回答

find(cellfun(@(x) isequal(x,1), C))

1 件のコメント

George Vuong
George Vuong 2015 年 7 月 27 日
編集済み: George Vuong 2015 年 7 月 27 日
Hey Walter, thanks for your response. The output was just what I was looking for. I understand that cellfun applies a function to each cell of a cell array. But can you provide a little explanation as to what @(x) isequal(x,1) does and means?
UPDATE: I think I figured it out. @x creates an anonymous function. And isequal(x,1) inspects each column to see if the value is 1.

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

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2015 年 7 月 25 日
編集済み: Andrei Bobrov 2015 年 7 月 25 日
C = {[], [], [], [], 1, 1, 1, 1, 1, 1, 4, 4, 5, 5, 5, [], [], []};
b = C;
b(cellfun(@isempty,b))={nan};
d = find([b{:}]==1);

1 件のコメント

Hey Andrei, thank you for your input. It's interesting how in order for
d = find([b{:}]==1);
to recognize that the null vectors, [], is a column, you have to input nan within [].

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

Cedric
Cedric 2015 年 7 月 25 日
編集済み: Cedric 2015 年 7 月 25 日
find( cellfun( @(x) ~isempty(x) && x==1, C ))
ans =
5 6 7 8 9 10

2 件のコメント

find(cellfun(@(x) isequal(x,1), C))
Cedric
Cedric 2015 年 7 月 25 日
編集済み: Cedric 2015 年 7 月 25 日
@Walter: I vote for your solution if you post it as an answer!

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

カテゴリ

ヘルプ センター および 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