Select all "77" intervals from matlab array

I have an array full of data, but the data intersect at 77 intervals (which have different lengths) like this

[-1, 2, 2, 4, -5, 77, 77, 77, 6, 5, 34, 77, 77, 4, 7...]

There is no problem to find the index of the beginning or end of this interval, but I need to save their indices with some kind of (?) Array (as I thought) is not suitable for this (but maybe there is a solution how to write this in the matrix, but the results have different sizes.so for input

[-1, 2, 2, 4, -5, 77, 77, 77, 6, 5, 34, 77, 77, 4, 7...]

expected response

[6,7,8]
[12,13]

starts with 1. How can I do this?

+4
source share
3 answers

, . F. , , , , .

A , , 77, find(). , . mat2cell() , diff() .

A = [-1, 2, 2, 4, -5, 77, 77, 77, 6, 5, 34, 77, 77, 4, 7];
B = find(A==77);             %// B = [6  7  8  12  13]
C = diff(B);                 %// C = [1  1  4  1]
D = find(C~=1);              %// D = [3]
E = diff([0 D length(B)]);   %// E = [3  2]
F = mat2cell(B,1,E);         %// F = [1x3 double] [1x2 double]

%// F{1} = [6   7   8]
%// F{2} = [12 13]
+1

diff arrayfun :

data = [-1, 2, 2, 4, -5, 77, 77, 77, 6, 5, 34, 77, 77, 4, 7]; %// example data

aux = diff(data==77); %// add dummy value at end, in case final data is 77
starts = find(aux==1)+1;
if data(1)==77 %// special case: start with a run
    starts = [1 starts];
end
ends = find(aux==-1);
if data(end)==77  %// special case: end with a run
    ends = [ends numel(data)];
end
result = arrayfun(@(n) starts(n):ends(n), 1:length(starts), 'uni', false);

:

>> result{1}

ans =

     6     7     8

>> result{2}

ans =

    12    13
+2

[6,8]
[12,13]

,

m=[-1, 2, 2, 4, -5, 77, 77, 77, 6, 5, 34, 77, 77, 4, 7]
r=[find([1,m]~=77&[m,1]==77);find([m,1]~=77&[1,m]==77)-1]'

, :

for bounds=[find([1,m]~=77&[m,1]==77);find([m,1]~=77&[1,m]==77)-1];
  disp(bounds(1):bounds(2))
end

Replace ~ = 77 with> = 0 and == 77 with <0 if you want to check negative numbers instead of 77.

+1
source

All Articles