【发布时间】:2017-08-14 06:54:09
【问题描述】:
我有一组单元格B。 B 的每个元素都有不同的行号。如何仅访问 行数大于阈值的B 元素?
我试过了:
A = B{cellfun('length', B) >= threshold};
这可以不用循环来完成吗?
【问题讨论】:
标签: matlab indexing cell-array
我有一组单元格B。 B 的每个元素都有不同的行号。如何仅访问 行数大于阈值的B 元素?
我试过了:
A = B{cellfun('length', B) >= threshold};
这可以不用循环来完成吗?
【问题讨论】:
标签: matlab indexing cell-array
要么
B(cellfun('length', B) >= threshold )
或
B(cellfun(@(x) numel(x) >= threshold, B))
应该可以。
它们都计算内部元胞数组中的元素。
如果你真的想要只有单元格,行数大于或等于阈值:
B(cellfun('size', B, 1) >= threshold )
或
B(cellfun(@(x) size(x, 1) >= threshold, B))
示例
arr_Row1Col1 = {1};
arr_Row1Col2 = {1,2};
arr_Row2Col1 = {1;2};
arr_Row2Col2 = {1,2;3,4};
threshold = 2;
B = {arr_Row1Col1, arr_Row1Col2, arr_Row2Col1, arr_Row2Col2};
% All inner-cells that have more than one element
B(cellfun('length', B) >= threshold )
% All inner-cells that have more than one row
B(cellfun('size', B, 1) >= threshold )
输出:
ans = {1x2 cell} {2x1 cell} {2x2 cell}
ans = {2x1 cell} {2x2 cell}
【讨论】: