Fill gaps using Nan in for loop
1 view (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Stephen23
on 20 Sep 2019
Edited: Stephen23
on 20 Sep 2019
>> 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
More Answers (1)
Shunichi Kusano
on 20 Sep 2019
Hi Jaehwi,
I wrote code. hope this helps.
data = [0 50; 1 100; 2 200; 3 300; 5 500; 6 600; 7 700; 10 1000; 0 30; 2 20; 6 40;];
% find where the value decreases
decreaseIndex = find(diff(data(:,1)) < 0);
% new object begin with decreasing the value
objectBeginIndex = [1, decreaseIndex+1];
%% loop for each objects
objectsNum = length(objectBeginIndex);
fullset = [0:10]';
fulldata = NaN(11*(objectsNum-1), 2); % up to the second last
fulldata(:,1) = repmat(fullset, objectsNum-1, 1);
for i = 1:objectsNum
if ~(i == objectsNum)
tempdata = data(objectBeginIndex(i):objectBeginIndex(i+1)-1,:); % extract single object
existDataIndex = find(ismember(fullset, tempdata(:,1))); % find where the data exist
fulldata((i-1)*11+existDataIndex, 2) = tempdata(:,2); % place the data
else % if i is last, up to existing number, not to 10.
tempdata = data(objectBeginIndex(i):end,:);
maxi = tempdata(end,1);
existDataIndex = find(ismember([0:maxi]', tempdata(:,1)));
lastdata = [[0:maxi]', NaN(maxi+1, 1)]; % up to the second last
lastdata(existDataIndex,2) = tempdata(:,2);
fulldata = [fulldata; lastdata];
end
end
See Also
Categories
Find more on Sequence Alignment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!