【问题标题】:Matlab error-"Input must be a cell array of strings"...how to get unique values from numeric cell..iff..matrix is of mixed valuesMatlab错误-“输入必须是字符串的单元格数组”...如何从数字单元格中获取唯一值..iff ..matrix是混合值
【发布时间】:2015-11-25 23:11:38
【问题描述】:

这是我的代码:

A={2,'small','low','unacc';2,'small','med','unacc';2,'small','high','unacc';4,'big','low','unacc';4,'big','high','acc';'more','big','med','acc';'more','big','high','vgood';4,'med','med','acc'};

我想从上面矩阵的第一列获取所有唯一值

uniques(A(:,1)) 报错

【问题讨论】:

  • A(:,1) 不是字符串元胞数组。你也有双打。
  • 那么如何从 this.any other solution 中获得独特性?
  • 只需在 2(或任何其他数字)之前和之后放置 ',然后它也被视为字符串。

标签: matlab


【解决方案1】:

正如@excaza 所说,您首先需要更改双值字符串

A={2,'small','low','unacc';2,'small','med','unacc';...
   2,'small','high','unacc';4,'big','low','unacc';...
   4,'big','high','acc'; 'more','big','med','acc';...
   'more','big','high','vgood';4,'med','med','acc'};

A_full_string = cellfun(@(x) num2str(x), A, 'UniformOutput',0);

基本上,num2str 接受一个数字或一个字符串作为输入,并将这个值作为一个字符串返回。 cellfun 将函数应用于元胞数组的每个元素。

现在你所有的值都是字符串值:

unique(A_full_string(:,1))

ans = 

    '2'
    '4'
    'more'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 2017-11-18
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多