Find an index of a cell whose element satisfies a condition
24 ビュー (過去 30 日間)
古いコメントを表示
Is there a way to economically fix the code below to make it work so that it returns the index of the cell element whose array has the number 1 in its second column?
c = {[1,2,3], [2,3,1], [3,1,2]};
find(c{:}(:,2)==1) % expected result is the cell index 3
The code above gets me this error:
Intermediate brace {} indexing produced a comma-separated list with 3 values, but it must produce a single value to perform subsequent indexing operations.
I came up with this one
find(~cellfun(@isempty,(cellfun(@(x) find(x(:,2)==1,1),c,'un',0))))
but I would like to find some other efficient ways of doing it.
0 件のコメント
採用された回答
Star Strider
2021 年 6 月 18 日
Try this —
c = {[1,2,3], [2,3,1], [3,1,2]};
idx = cellfun(@(x)x(:,2)==1, c, 'Unif',0)
Out = find([idx{:}])
.
0 件のコメント
その他の回答 (1 件)
DGM
2021 年 6 月 18 日
This is one way:
c = {[1,2,3], [2,3,1], [3,1,2], [1,2,3], [2,3,1], [3,1,2]};
idx = find(cellfun(@(x) x(2)==1,c))
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!