Find the different elements in a cell array

Hi to all,
i have a problem because i have a cell array and i need to find the cells that contain both numbers 1 and 2.
Thanks.

 採用された回答

Stephen23
Stephen23 2021 年 11 月 23 日
編集済み: Stephen23 2021 年 11 月 23 日

0 投票

Where out is your cell array:
fnh = @(v) any(v(:)==1)&&any(v(:)==2);
idx = cellfun(fnh,out)

12 件のコメント

LL
LL 2021 年 11 月 23 日
It gave me error of invalid expression for the fnh line.
Stephen23
Stephen23 2021 年 11 月 23 日
@Lidia Frizzi: fixed now, please try it again.
LL
LL 2021 年 11 月 23 日
it works thank you!!!!
LL
LL 2021 年 11 月 26 日
@Stephen sorry could i please ask you, how can i find all the cell without the number 2?
Stephen23
Stephen23 2021 年 11 月 26 日
fnh = @(v) all(v(:)~=2);
LL
LL 2021 年 11 月 26 日
Perfect!!!
Now i have a matrix with only 1 (when the condition is true) and 0; but if i would like to maintain in the true cells the contents of the initial cell array? how could i do this?
Stephen23
Stephen23 2021 年 11 月 26 日
What should the output array contain for the false indices?
LL
LL 2021 年 11 月 26 日
Maybe 0
Stephen23
Stephen23 2021 年 11 月 26 日
you could replace the cells in the existing cell array:
out(~idx) = {0};
or create a new cell array:
new = num2cell(idx);
new(idx) = out(idx)
LL
LL 2021 年 11 月 29 日
@Stephen i still need your help pls.
I have one cell array like this one, what i like to do, is to merge the contents of neighboring cells, other than zero.
Stephen23
Stephen23 2021 年 11 月 29 日
@Lidia Frizzi: please show the expected result of that "merging".
LL
LL 2021 年 11 月 29 日
So for example in the rows 6-7 and colums 8-9 i have three cells not empty the result that i would like to obtain is like this.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

LL
2021 年 11 月 23 日

コメント済み:

LL
2021 年 11 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by