Find location (column) of a [1] in a cell array

2 ビュー (過去 30 日間)
Lauren Harkness
Lauren Harkness 2017 年 10 月 19 日
コメント済み: Karleigh Irwin 2017 年 10 月 20 日
If I have a 1x3 cell array that looks something like this [] [] [1], or this [1][][], how would I get my code to print the index at which the 1 is? so for the first example, it's in column 3 and I would like to output a 3, and for the second example the 1 is in column 1/position 1 so I would like to output a 1. (Note: this will always be for a 1xN cell array with only one row)
  1 件のコメント
Karleigh Irwin
Karleigh Irwin 2017 年 10 月 20 日
Good question!!!

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 19 日
find(~cellfun(@isempty, YourCell))
Those kinds of arrays are common when you are using strfind() or regexp().
It is common in such cases that using a logical vector does as well or better than finding the index, especially if there might be multiple matches:
mask = ~cellfun(@isempty, YourCell);
selected = YourOriginal(mask);
and not infrequently
mask = cellfun(@isempty, YourCell);
YourOriginal(mask) = [];
leaving only the matching entries.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by