【问题标题】:How to select the rows based on number of columns in a cell matrix?如何根据单元格矩阵中的列数选择行?
【发布时间】:2023-12-31 23:34:01
【问题描述】:

我有一个大小为 248*15 的单元矩阵,最大列数为 15。我想在 MatLab 中提取包含大于或等于 8(>=8) 个非零列条目的行。

例如:单元格行 1,2,7,8,.....

Find attached image

【问题讨论】:

  • 为什么投反对票?这是一个有效的问题。但是,添加示例代码或您迄今为止尝试过的内容是指示性的,并且显示了努力。在下面检查我的解决方案,让我知道它是否有效。

标签: matlab matrix cell


【解决方案1】:

您可以使用cellfun先判断哪些单元格元素为空,然后根据需要使用数组索引选择行:

C = {} % The cell matrix of size 248 x 15.

% An array of 248 x 15 that has Booleans based on empty or not:
emptyCells = cellfun(@isempty, C)

% The total number of empty columns on each row:
emptyColsCount = sum(emptyCells, 2)

% Find those rows with at least 8 non-zero columns
requiredRowIndices = find(emptyColsCount < 8)
% This returns [1, 2, 7, ...]

【讨论】:

    最近更新 更多