【问题标题】:Identifying uniques in a cell array识别元胞数组中的唯一值
【发布时间】:2011-01-24 10:28:33
【问题描述】:

我在MATLAB 中有一个 45x2 单元格,第一列是任意大小的双精度矩阵。

其中一些矩阵是重复的,而另一些则不是。我试图只去除唯一的矩阵(但记录重复次数),并保持第二列不变。

我尝试了很多方法(表格、hist 等),但由于细胞结构(我认为),它们都失败了。如果不逐个循环遍历它们,该怎么做呢?

【问题讨论】:

    标签: matlab cells


    【解决方案1】:

    如果将矩阵转换为字符串,则可以对它们运行 unique:

    %# create a sample cell array
    mc = {magic(3);magic(4);magic(4);magic(5);magic(3);magic(4)}
    
    %# convert to strings
    mcs = cellfun(@(x)(mat2str(x)),mc,'uniformoutput',false);
    
    %# run unique
    [uniqueCells,idxOfUnique,idxYouWant] = unique(mcs);
    

    【讨论】:

    • 注意第二行可以简化为mcs = cellfun(@mat2str, mc, 'UniformOutput', false);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2014-12-26
    • 1970-01-01
    • 2017-03-27
    • 2011-07-17
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多