repeat5.m

来自「matlab7.0这本书的源代码」· M 代码 · 共 27 行

M
27
字号
% repeat5.m
% repeated value creation and counting
% inverse operation

% y = [3 3 0 0 0 5 6 6]; % data to examine

x = zeros(size(y));    % preallocate results
n = zeros(size(y));

x(1) = y(1);             % beginning data
n(1) = 1;                % beginning count
idx = 1;                 % index value
for i=2:length(y)
   if y(i)==x(idx) % value matches current x
      
      n(idx) = n(idx)+1; % increment current count
      
   else % new value found
      
      idx = idx+1;       % increment index
      x(idx) = y(i);     % poke in new x
      n(idx) = 1;        % start new count
   end
end
nz = (n==0);  % find elements not used
x(nz) = [];   % delete excess allocations
n(nz) = [];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?