MATLAB: Fill gaps using Nan in for loop

analysisbig datanan valuetime series

I have a time series with some gaps in it and i want to fill the gaps with NaN in loop.
For example, my data is
data = [0 50;
1 100;
2 200;
3 300;
5 500;
6 600;
7 700;
10 1000;
0 30;
2 20;
6 40]
First column and second column refer to time and signal value. But it has 2 objects. (I have thousands objects.). One was begun from 0(first row), the other one was begun 0(9th row). But sometimes it began from 2 or whatever. The solution code should detect the next result when the value of 1st column decreased.
I'd like to change it as a new_data.
new_data = [1 100;
2 200;
3 300;
4 NaN;
5 500;
6 600;
7 700;
8 NaN;
9 NaN;
10 1000;
0 30;
1 NaN;
2 20;
3 NaN;
4 Nan;
5 Nan;
6 40]
How can I make it?
If anyone can help, it would be greatly appreciated.
Thank you!

Best Answer

Using two accumarray calls:
>> data = [0,50;1,100;2,200;3,300;5,500;6,600;7,700;10,1000;0,30;2,20;6,40]
data =
0 50
1 100
2 200
3 300
5 500
6 600
7 700
10 1000
0 30
2 20
6 40
>> D = diff(data(:,1),1,1);
>> F = @(v){(v(1):v(end)).'};
>> M = [cell2mat(accumarray(cumsum([true;D<0]),data(:,1),[],F)),accumarray(cumsum([1;max(1,D)]),data(:,2),[],[],NaN)]
M =
0 50
1 100
2 200
3 300
4 NaN
5 500
6 600
7 700
8 NaN
9 NaN
10 1000
0 30
1 NaN
2 20
3 NaN
4 NaN
5 NaN
6 40