在Matlab中删除重复标签的行

时间:2017-10-12 13:48:12

标签: matlab unique cell rows indices

在Matlab中,我有一个单元格,其中第一列由标记组成,其中标记太多。我们想要删除这些无关标记的整个细胞行。

我们在单元格的第一列中有四种标记类型 - '71', '72', '73' and '74':存储为字符串。每当出现任何这些标记时,都会重复这些标记('71' '72' 23次,'73' '74' 16次)。我们希望每次出现标记时都保留第一个演示文稿。然后,'71' '72'保持每第6个,而'73' '74'保持每第4个。

单元格的长度为550,因此71, 72, 73 and 74的块在单元格中重复多次,除此之外还有其他标记也存在,我们希望保留。

细胞的一小部分如下所示:

'12'    14737   29472
'31'    44747   89492
'4'     44771   89540
'53'    53756   107510
'73'    53831   107660
'73'    54082   108162
'73'    54331   108660
'73'    54582   109162
'73'    56081   112160
'73'    56331   112660
'73'    56581   113160
'73'    56831   113660
'73'    58330   116658
'73'    58580   117158
'73'    58829   117656
'73'    59079   118156
'73'    60579   121156
'73'    60829   121656
'73'    61079   122156
'73'    61329   122656
'63'    351340  702678
'4' 351361  702720
'54'    360342  720682
'74'    360375  720748
'74'    360633  721264
'74'    360883  721764
'74'    361133  722264
'74'    362632  725262
'74'    362882  725762
'74'    363132  726262
'74'    363382  726762
'74'    364881  729760
'74'    365131  730260
'74'    365381  730760
'74'    365631  731260
'74'    367130  734258
'74'    367380  734758
'74'    367630  735258
'74'    367880  735758
'64'    369374  738746
'4' 369379  738756
'51'    378376  756750
'71'    378409  756816
'71'    378584  757166
'71'    378750  757498
'71'    378917  757832
 **continued**

我不知道如何做到这一点,我试图使用索引来解决这个问题,但到目前为止还没有成功。

有人可以帮忙吗?

由于

到目前为止我尝试过:

function Y = correctRows(y)
Y.type = {};
Y.latency = [];
Y.latency_ms = [];


for i = 1:length(y)

if strcmp(y(i).type,'71') || strcmp(y(i).type,'72')
   Y(i) = y(i); 
    i = i+5;

elseif strcmp(y(i).type,'73') || strcmp(y(i).type,'74')
    Y(i) = y(i); 
    i = i+3;
else
    Y(ii) = y(i);
end 
end

2 个答案:

答案 0 :(得分:0)

这是一个带数值数组而不是字符串数组的例子。

我认为你自己获得了转换。你的主要想法并不差。但你没有定义ii,我宁愿使用while而不是for循环。

此代码仅演示如何取消数字Vector中的某些行。转移到任何其他矩阵/单元应单独处理

clear all;
A=[12
31
4
53
73
73
73
73
73
73
73
73
73
73
73
73
73
73
73
73
63
4
54
74
74
74
74
74
74
74
74
74
74
74
74
74
74
74
74
64
4
51
71
71
71
71];

现在代码

counter=1;
Result=zeros(size(A));
i=1;
while k<=length(A)

if (A(k)==71 || A(k)==72)
    Result(counter,:) = A(i);
    k = k+6;
    counter=counter+1;
elseif (A(k)==73 || A(k)==74)
    Result(counter,:) = A(k);

    k = k+4;
    counter=counter+1;
else
    Result(counter,:) = A(k);
    k = k+1;
    counter=counter+1;
end

end
%delet unused rows
Results(counter:end)=[];

答案 1 :(得分:0)

我使用通用地图寻求可扩展的方法(A是您的单元格):

%       markers      how-manieth   block size
map = { {'71' '72'}  6             23
        {'73' '74'}  4             16 };

for ii = 1:size(map,1)

    % Extract block
    blk  = cellfun(@(x) any(strcmp(x,map{ii,1})), A(:,1));

    % Find the how-manieth entry in each block
    inds = (mod(cumsum(blk), map{ii,3}) == map{ii,2});

    % Empty the rest
    A(xor(blk, inds),:)  = {[]};

end

% Remove all entries marked for removal
A = A(~cellfun('isempty',A(:,1)),:);

如果还有其他可能重复的键,您只需要保留第一个键,那么这可能更具可读性:

%       markers      how-manieth 
map = { {'71' '72'}, 6
        {'73' '74'}, 4 };

% The routine
output = cell(size(A));
index  = 1;

repetitions = 0;
repcount    = 0;
have_match  = false;

previous = '';

for ii = 1:size(A,1)

    % Current label
    current = A(ii,1);

    % If this label equals the previous one
    if strcmp(current, previous)

        repetitions = repetitions + 1;

        % First entry: 
        if repetitions==1

            % Check for mapped labels
            match = cellfun(@(x)any(strcmp(current,x)), map(:,1));
            if any(match)      
                % Mapped label found: 
                index      = index-1;
                repcount   = map{match,2};
                have_match = true;
            end

        % Subsequent entries
        elseif have_match && repetitions==repcount-1        
            output(index,:) = A(ii,:);
            index           = index + 1;
        end

    % Current label is not repeated; just insert the new data
    else
        have_match   = false;
        repcount     = 0;
        repetitions  = 0;

        output(index,:) = A(ii,:); 
        index           = index + 1;
    end

    previous = current;    
end

% Chop-off redundant entries
output(index:end,:) = [];