cut vector according to NaN values

487 views Asked by At

data_test is a vector that is populated by numbers with some NaN.

data_test = [NaN, 2, 3, 4, NaN,NaN,NaN, 12 ,44, 34, NaN,5,NaN];

I would like to cut data_test according to the NaNs and create a cell array containing the pieces of data_set in between NaNs.

data_cell{1}=[2 3 4];
data_cell{2}=[12 44 34];
data_cell{3}=[5];

at this point I need to filter these values (this is OK, just as an example the filtered values will be the same of data_test +1)

data_cell{1} -> data_cell_filt{1}
data_cell{2} -> data_cell_filt{2}
data_cell{3} -> data_cell_filt{3}

and put back the filtered values in data_test.

data_cell_filt{1}
data_cell_filt{2} -> data_test
data_cell_filt{3}

in order that data_test is

data_test = [NaN, 3, 4, 5, NaN,NaN,NaN, 13 ,45, 35, NaN, 6, NaN];

ps (data_test in my case is ~20000 elements)

3

There are 3 answers

2
Dan On BEST ANSWER

You can do it easily with a loop or use arrayfun like this:

A = [NaN, 2, 3, 4, NaN, NaN, NaN, 13, 45, 35, NaN, 6, NaN]

i1 = find(diff(isnan(A))==-1)+1  %// Index where clusters of numbers begin 
i2 = find(diff(isnan(A))==1)     %// Index where clusters of numbers end

data_cell_filt = arrayfun(@(x,y)({A(x:y)}),i1,i2 ,'uni', false)
0
Luis Mendo On

Convolution-based approach:

ind = isnan(data_test);
t = conv(2*x-1, [-1 1], 'same'); %// convolution is like correlation but flips 2nd input
starts = find(t==2); %// indices of where a run of non-NaN's starts, minus 1
ends = find(t==-2);  %// indices of where it ends
result = mat2cell(data_test(~ind), 1, ends-starts); %// pick non-NaN's and split
1
Santhan Salai On

One approch with accumarray and cumsum and diff

%// find the index of regular numbers
idx = find(~isnan(data_test))

%// group the numbers which are adjacent, to some index number
idx1 = cumsum([1,diff(idx)~=1])

%// put all those numbers of same index number into a cell
out = accumarray(idx1.',data_test(idx).',[],@(x) {x.'})

Sample run:

data_test = [NaN, 2, 3, 4, NaN,NaN,NaN, 12 ,44, 34, NaN,5,NaN];

>> celldisp(out)
out{1} =
 2     3     4

out{2} =
12    44    34

out{3} =
 5