How to find values greater than a number in a cell of empty cells?

21 ビュー (過去 30 日間)
BR
BR 2019 年 11 月 8 日
編集済み: BR 2019 年 11 月 11 日
Hello everyone,
I am trying to find the values greater than '0' in a cell array of few empty cells eg. D = {[],[],[3],[],[],[]}?
I have tried multiple options like
  1. find(D{:}>=0)
  2. [row,col] = cellfun(@(c) find(c >= 0), D, 'uniform', false)
  3. for i = 1 : length(Match), idx(i) = find(Match{1,i}>=0);end
  4. ismember(D,3);
But nothing has worked so far?
All the best,
Thanks

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 8 日
編集済み: Adam Danz 2019 年 11 月 9 日
D = {[],[],3,[], -2, 0, [], 1, 6, []};
idx = cellfun(@(x)~isempty(x) && x >= 0, D); % logical index
If you need the subscript index,
idx = find(cellfun(@(x)~isempty(x) && x >= 0, D)); % subscript index
If D contains non-scalars,
D = {[],[],3,[], -2, 0, [], 1, 6, [], -2:2, magic(4)-8};
idx = cellfun(@(x)~isempty(x) & x >= 0, D, 'UniformOutput', false);
but now idx is a cell-array so to find the values >= 0 in element n of D, idx{n}.
  10 件のコメント
Adam Danz
Adam Danz 2019 年 11 月 9 日
編集済み: Adam Danz 2019 年 11 月 9 日
This line sort(cellfun(@(x)x(1),C)) merely extracts the first value within each element of the cell array C. The elements of C can be scalar, vectors (rows or columns), matricies, multi-dim arrays, any numeric format. But when in comes across an empty element it throws an error because there is no first-value to extract.
BR
BR 2019 年 11 月 11 日
編集済み: BR 2019 年 11 月 11 日
Ok, thanks man. Appreciate it and sorry for the late response.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by