If I have a vector:
[4,5,6,7,11,12,13,14,21,22,23]
How can I extract the start / end values ββof all consecutive numeric blocks without a loop, i.e. the desired result for the above vector will be a two-column vector:
b = 4 7 11 14 21 23
Easily:
a = [4,5,6,7,11,12,13,14,21,22,23]; b = reshape(a(sort([find(a - circshift(a,[0,1]) ~= 1),find(a - circshift(a,[0,-1]) ~= -1)])),2,[])'
Output:
Another approach:
x = [4,5,6,7,11,12,13,14,21,22,23]; x = x(:); ind = find([1; diff(x)-1; 1]); result = [x(ind(1:end-1)) x(ind(2:end)-1)];
:
v = [4,5,6,7,11,12,13,14,21,22,23]; dv = diff(diff(v)==1); bv = find(dv==+1)+1; if dv(1) == 0 bv = [1,bv]; end; ev = find(dv==-1)+1; if dv(end) == 0 ev = [ev,numel(v)]; end; b = v([bv(:),ev(:)]);
, , , :
x = (0:10:a(end))'; %' subindex = @(A) [A(1) A(end)]; fun = @(q) subindex( a(a>q & a<q+10)); res = cell2mat(arrayfun(fun, x, 'UniformOutput', false));
a.
a
Source: https://habr.com/ru/post/1618274/More articles:cudaMemcpyFromSymbol on the __device__ variable - gpubatch filling with various sql queries - javaElimination of rejection ViewControllerAnimated only works after pressing TableView Row - iosPreparedStatement package with various SQL statements prepared - javaCan you make it impossible to close this window in Matlab? - matlabNegative value in string based C # calculator - stringAndroid - Skobbler does not support 64-bit processor - skmapsQuick Sort - sortingUIImageView black on startup screen - iosHow to save my application settings? - c #All Articles