Advertisement

MATLAB 编程实例练习(29):数组中元素出现次数的计数

阅读量:

题目:

源自Mathwork平台的Cody,Problem 55 - Counting Sequence,

在这里插入图片描述

代码的编写与实现,

复制代码
    function y = CountSeq(x)
    if length(x)==1
    y=[1,x];
    else
      a=1;b=x(1);y=[];
      for i=2:length(x)
            if x(i)==x(i-1)
                a=a+1;
            else
                y=[y,[a,b]];
                a=1;
                b=x(i);
            end  
      end
    
    
      if x(end)==x(end-1)
          y=[y,[a,b]];
      else
          y=[y,[1,x(end)]];
      end
    end   
    end

全部评论 (0)

还没有任何评论哟~