1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- function [c1cv, isq1s, isq1e, xv_min_seq_length] =countconsecutiveones(xv,zero_pad,min_seq_length)
- if nargin <2
- zero_pad=0;
- if nargin <3
- min_seq_length=1;
- end
- end
- isq1s=nan(size(xv));
- isq1e=nan(size(xv));
- c1cv=nan(size(xv));
- xv_min_seq_length=zeros(size(xv));
- if isvector(xv)
- if isrow(xv)
- xv=xv';
- end
- if zero_pad
- xv(isnan(xv))=0;
- else
- xv(isnan(xv))=[];
- end
- %fv = find(abs(diff([0; xv; 0]==1)))
- %c1cv = fv(2:2:end)-fv(1:2:end-1);
- xdiff=diff([0; xv; 0]);
- i1sct = find(xdiff==+1); %indices where ones sequence start
- i1ect = find(xdiff==-1); %indices where ones sequence end
- c1ct = i1ect - i1sct;
-
- isq1s(1:length(i1sct))=i1sct;
- isq1e(1:length(i1ect))=i1ect;
- c1cv(1:length(c1ct))=c1ct;
- isq1se=[isq1s(c1cv>min_seq_length) isq1e(c1cv>min_seq_length)];
- if ~isempty(isq1se)
- for jj=1:size(isq1se,1)
- xv_min_seq_length(isq1se(jj,1):isq1se(jj,2)-1)=1;
- end
- end
-
- elseif ismatrix(xv)
- if isscalar(min_seq_length)
- min_seq_length=repmat(min_seq_length,size(xv,2));
- end
- xv_min_seq_length=nan(size(xv));
- for jj=1:size(xv,2)
- [c1ct,i1sct,i1ect,xvmslt]=countconsecutiveones(xv(:,jj),zero_pad,min_seq_length(jj));
- isq1s(1:length(i1sct),jj)=i1sct;
- isq1e(1:length(i1ect),jj)=i1ect;
- c1cv(1:length(c1ct),jj)=c1ct;
- xv_min_seq_length(:,jj)=xvmslt;
- end
- %c1(all(isnan(c1),2),:)=[];
- else
- error('input must be a vector or matrix');
- end
|