📄 blocknum.m
字号:
function[num,a,b]=blocknum(x,delta)%BLOCKNUM Numbers the contiguous blocks of an array.%% Suppose X is a column vector which contains blocks of identical% values, e.g. X=[0 0 0 1 1 3 2 2 2]';%% N=BLOCKNUM(X) gives each contiguous block of identical values a% unique number, in order beginning with 1. Each elements of N% specifies the number of the block to which the corresponding% element of X belongs.%% In the above example, N=[1 1 1 2 2 3 4 4 4]';% % [N,A,B]=BLOCKNUM(X) also returns arrays A and B which are indices% into the first and last point, respectively, of each block. In% the above example, A=[1 4 6 7]' and B=[3 5 6 9]';%% [...]=BLOCKNUM(X,D) defines the junction between two blocks as% locations where ABS(DIFF(X))>D. Thus D=1 is a 'rate of change'% definition, and X=[1 2 3 5 6 10 16 17 18]'; will yield the same % result for N as in the previous example.%% Usage: num=blocknum(x);% [num,a,b]=blocknum(x);% _________________________________________________________________% This is part of JLAB --- type 'help jlab' for more information % (C) 2000, 2004 J.M. Lilly --- type 'help jlab_license' for details if strcmp(x,'--t') blocknum_test;return;endif nargin==1 delta=0;enddx=diff(x);index=find(abs(dx)>delta);num=zeros(size(x));if ~isempty(index) num(index+1)=1;endnum(1)=1;num=cumsum(num);if nargout>=2 a=[1;find(diff(num)~=0)+1];endif nargout>=3 b=[find(diff(num)~=0);length(num)];end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function[]=blocknum_testx= [0 0 0 1 1 3 2 2 2]';y= [1 1 1 2 2 3 4 4 4]';reporttest('BLOCKNUM with D==0',all(y==blocknum(x)))x=[1 2 3 5 6 10 16 17 18]';reporttest('BLOCKNUM with D==1',all(y==blocknum(x,1)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -