【问题标题】:Frequency for cells in Matlab?Matlab中单元格的频率?
【发布时间】:2015-06-12 09:08:25
【问题描述】:

我在 Matlab A 中有一个尺寸为 mx1单元格,例如m=11

 A={1 2 2 4 5 5 6 3 1 1 2}

DA中唯一元素的向量,即

D=unique(A,'stable')

给了

D={1 2 4 5 6 3}

我想找到C,它给出了D中每个元素的重复次数,即

C={3 3 1 2 1 1}

我试过了

count=histc(A,D);

但它告诉我

"Error using histc
First Input must be a real non-sparse numeric array."

如果我尝试使用cell2mat 在矩阵中转换AC,它会给我

"Error using cat
Dimensions of matrices being concatenated are not consistent."

有什么想法吗?

【问题讨论】:

  • D=unique(A,'stable') 在我的机器 (R2015a) 中出现错误。要应用 uniquehistcA 必须是数字向量(不是数字元胞数组)。所以首先使用A = [A{:}]。此外,histc 的第二个输入应该是非递减的。所以你可以使用sum(bsxfun(@eq, A(:), D(:).'))而不是histc
  • 您还有其他不需要将 A 转换为数字向量的建议吗?在我的实际代码中,A 的每个元素都是一个 5 位数的数字,执行 A=[A{:}] 会产生一个 char 1x(将所有数字相邻的总位数)。
  • 我在使用unique(A,'stable') 时也遇到了一个错误——尽管cell2mat(A) 对我来说非常好用。 - 也许您的单元格中的数据与您给出的示例不同?例如具有超过 1 个元素的矩阵/向量?

标签: matlab


【解决方案1】:

Matlab 推荐 histcount 而不是 histc

a = cell2mat(A) ;
count=histcounts(a, max(a) - min(a))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多