【问题标题】:Deleting rows of repeated labels in Matlab在Matlab中删除重复标签的行
【发布时间】:2018-03-24 11:49:00
【问题描述】:

在 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

【问题讨论】:

  • 你能分享你目前开发的代码吗?还有一个输出示例
  • @Irreducible 我已将其添加到问题中
  • 7172 永远不会互相关注? 7374 的相同问题
  • @Irreducible 它们都是随机的,任何一个都可以跟随另一个
  • 你写了“我们希望每次出现标记时都保留第一个演示文稿。” 然后在下一行中,你用这些话自相矛盾:“那么对于 '71' '72' 保持每 6 次,而对于 '73' '74' 保持每 4 次。”

标签: matlab unique cell rows indices


【解决方案1】:

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

我认为您可以自己转移到字符串。你的主要思想还不错。但是你没有定义ii,我宁愿使用while而不是for循环。

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

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)=[];

【讨论】:

  • 谢谢,这适用于数组,但不适用于单元格。它给我一个错误,因为我有一个单元格而不是一个数组......
  • 输出也是单元格吗?您遇到了哪个错误?
  • 这是错误:未定义运算符 '==' 用于类型为“cell”的输入参数。
  • 在您的问题中,请添加一个单元格输出数据的示例
  • 我不明白你的意思...我提供了我的单元格的示例,这就是我需要能够使用的。
【解决方案2】:

我会选择使用通用地图的可扩展方法(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,:) = [];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2019-11-15
    • 2012-12-29
    相关资源
    最近更新 更多