【问题标题】:junk, index and unique on a matrix (how to keep matrix format)矩阵上的垃圾,索引和唯一性(如何保持矩阵格式)
【发布时间】:2011-12-08 13:35:28
【问题描述】:

在 8x8 矩阵上使用此方法:

>> [junk,index] = unique(data,'first');        %# Capture the index, ignore junk
>> data(sort(index))                           %# Index data with the sorted index

以 64x1 格式(如果未找到重复)或 nx1(如果找到一些重复)输出格式。

我的问题是如何在没有排序的情况下保持矩阵格式?

我需要它来检查唯一(行)是否存在重复的非唯一单元格。并删除重复的行但保留格式(不要排列/排序)。

【问题讨论】:

  • 使用~ 而不是junk 不收集您不需要的输出。

标签: matlab sorting matrix duplicates unique


【解决方案1】:

如果您想要唯一的行,同时保持原始顺序,试试这个:

[M,ind] = unique(data, 'rows', 'first');
[~,ind] = sort(ind);
M = M(ind,:);

例子:

>> data = randi(2,[8 3]);
data =
     1     2     1
     1     2     1
     1     1     2
     2     2     2
     1     1     1
     2     2     2
     2     2     2
     2     1     1
>> M
M =
     1     2     1
     1     1     2
     2     2     2
     1     1     1
     2     1     1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2013-11-18
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多