【问题标题】:Identify if a column is repeated sequentially N times in a Matrix识别列是否在矩阵中按顺序重复 N 次
【发布时间】:2014-06-26 14:00:03
【问题描述】:

我知道有人以不同的方式提出了这个问题:

MATLAB: Identify if a value is repeated sequentially N times in a vector

我想知道如果我正在处理一个矩阵坐标,我如何才能准确地跟随上链接,例如:M = [ x1 x2 x3 x4 ... xn; y1 y2 y3 y4 ... yn]

例如 n=3 次:

M=  [ 2 3 3 3 1 4 4 4 6 6 6 6 8; 1 2 2 7 9 5 5 5 4 4 3 3 2]

ans: [ 4 4 4; 5 5 5] at position 6

我正在寻找一个“按顺序”重复 3 次的列。 在提到的链接中,有一种方法可以为向量执行此操作。

【问题讨论】:

  • 您能否进一步扩展您的问题。您是否希望为已知值或任何值执行此操作?
  • @MZimmerman6 谢谢,我编辑了我的问题。
  • 这样的栏目可以有多个

标签: matlab matrix


【解决方案1】:

试试这个

k = size(M,1);

%// fg is a matrix with sum of differences of n continuous columns\
fg = conv2(diff(M'),ones(k,n-1))';

%// coll is a row vector with column indices where fg is zero
[roww coll] = find(fg(k:size(fg,1)-k+1,n-1:end - n) == 0);

%// out_cell is a cell whose entries are the required matrices
out_ = arrayfun(@(x)(M(:,x:x+n-1)),coll,'UniformOutput',0);

out_ 将是一个包含matrices with size k x n 的单元格,其中的列同时出现n

【讨论】:

    【解决方案2】:

    基于字符串的方法 -

    %// Input (slightly different than yours to properly test out the code
    M=  [ 2 3 3 3 1 4 4 4 6 6 6 6 6 6; 1 2 2 7 9 5 5 5 4 4 3 3 3 3] 
    N = 3; %// Number of repetitions
    
    match = ['0' num2str(ones(1,N-1),'%1d') '0']; %// string match
    ind = strfind(['0' num2str(all(diff(M,[],2)==0),'%1d') '0'],match) %// output
    

    输出 -

    M =
         2     3     3     3     1     4     4     4     6     6     6     6     6     6
         1     2     2     7     9     5     5     5     4     4     3     3     3     3
    ind =
         6
    

    【讨论】:

      【解决方案3】:

      M的每一行翻译成唯一的数字标签:

      [~, ~, m ] = unique(M.','rows');
      

      现在您可以使用向量m 而不是矩阵M,也就是说,将您对向量的解决方案应用于m。结果索引直接对应M的行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        相关资源
        最近更新 更多