repeat3.m

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

M
16
字号
% repeat3.m
% repeated value creation and counting

%x = [3 2 0 5 6];  % data to repeat
%n = [2 0 3 1 2];  % repeat counts

nz = n==0;            % locations of zero elements
n(nz) = [];           % eliminate zero counts
x(nz) = [];           % eliminate corresponding data

csn = cumsum(n);               % cumulative sum of counts
tmp = zeros(1,csn(end));       % preallocate memory
tmp([1 csn(1:end-1)+1]) = 1;   % poke in ones
idx = cumsum(tmp);             % index vector
y = x(idx);                    % let array indexing do the work

⌨️ 快捷键说明

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