MATLAB Answers

## How I can create a vector with specified blanks?

Asked by Rita

### Rita (view profile)

on 5 Jul 2016
Latest activity Edited by Image Analyst

### Image Analyst (view profile)

on 7 Jul 2016
Accepted Answer by Image Analyst

### Image Analyst (view profile)

Hi,I have a time series of daily data for 4 years(4*365 data) and I would like to have a some of daily data to be blanks(gaps) to do some analysis on the rest. My problem is that how I can create a specified length gaps,for example 3 days,on my vector data.Is there any script to create artificial gaps with specific gap length?

#### 0 Comments

Sign in to comment.

## 3 Answers

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 6 Jul 2016
Edited by Image Analyst

### Image Analyst (view profile)

on 7 Jul 2016
Accepted Answer

Rita: Seems like you're having trouble so I put together this code for you:
% First we need to create sample data.
data = randi(9, 1, 300) % Create sample data.
% Get location of nans to put into the original sample data.
originalNanLocations = sort(randperm(length(data)-3, 10)) % Make 10 originall nans
data(originalNanLocations) = nan;
% Now we have our original data and we can begin...
% Find out where the original nans are, so we don't place other nans next to them
originalNanLocations = isnan(data);
% Find out how many nans we need to be in there.
% It should be about 10% of the number of elements.
numRequiredNans = floor(0.1 * length(data))
% Assign nans. Note, there may be overlap so that some stretches may be more than 3 long.
numNansCurrently = 0;
% Create a failsafe so we don't get into an infinite loop
maxIterations = 1000000;
iterationNumber = 1;
% Now loop, placing nans, until we get the required number of nans in the data.
while numNansCurrently < numRequiredNans && iterationNumber <= maxIterations
% Get location of starts of 15 "new" nan runs
nanStartLocation = randi(length(data)-4) + 1;
% Find the ending indexes of each run
nanEndLocation = nanStartLocation + 2;
% Get data from the start to the end plus one on either side of it.
% so we don't place other nans next to them,
% since that would create a stretch of 4 or more.
thisData = data((nanStartLocation-1):(nanEndLocation+1));
% Now find if out where the original nans are,
if any(isnan(thisData))
% Nans were found - skip this stretch of data.
continue;
end
% If we get here no nans were found in that location
% and we are free to assign new nans there.
data(nanStartLocation:nanEndLocation) = nan;
% Count the number of nans we have
numNansCurrently = sum(isnan(data));
iterationNumber = iterationNumber + 1;
end
data % Print to command window.
% Now the original nans will still be there and not be adjacent
% to any of the "new" nan stretches of three nans in a row.
% And the overall number of nans will be 10% of the elements
% or not more than 2 more than that.

Rita

### Rita (view profile)

on 6 Jul 2016
Thank you so much Image Analyst I really appreciate your help.

Sign in to comment.

### Azzi Abdelmalek (view profile)

Answer by Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

on 5 Jul 2016

You can set your data to nan
Data=rand(4*365,1) % your data
idx_gap=10:12
Data(idx_gap)=nan

Rita

### Rita (view profile)

on 5 Jul 2016
Thanks Azzi. How can I say for example 35% of my daily data goes nan. in the way that 3 days gap length repeated in my data.For example if I have A=[2 4 52 23 7 5 9 678 567...]the desired vector with 3 days gap B=[2 nan nan nan 7 5 9 678 567...]
Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

on 5 Jul 2016
Data=randi(10,20,1) % your data
freq=3
period=6
ii=1:period:numel(Data)
idx=bsxfun(@plus,ii',1:3)
Data(idx)=nan
Rita

### Rita (view profile)

on 5 Jul 2016
Thanks Azzi . How can I change period to percent of data it means that for example 10 % of data ?can I just divide my data to percent (10% of 4*365)? and one more question. If I also have the original Nans in my data and don't want to overlap with the "new nans" what should I do?Thanks a lot.

Sign in to comment.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 5 Jul 2016

Try this:
data = randi(9, 1, 300) % Create sample data.
% Get location of starts of 15 nan runs
nanStartLocations = sort(randperm(length(data)-3, 15))
% Find the ending indexes of each run
nanEndLocations = nanStartLocations + 3
% Assign nans. Note, there may be overlap so that some stretches may be more than 3 long.
for k = 1 : length(nanStartLocations)
data(nanStartLocations(k):nanEndLocations(k)) = nan;
end
data % Print to command window.

Rita

### Rita (view profile)

on 5 Jul 2016
Thanks Image Analyst.It creates 4 nans instead of 3 nans .should I replace "3" with "2" ? and as I mentioned to Azzi If I have original Nans in my vector how I can change the script in the way that "original nans" not overlay with the "new nans".Thanks a lot.
Image Analyst

### Image Analyst (view profile)

on 5 Jul 2016
If you want to make sure that no runs of 3 nans overlay with any other run of 3 nans, then you're going to have to check for that while you're assigning them. Change the for to a while and then check the elements to see if any are already nan. If any are, then use "continue" to skip to the bottom of the while loop. If none are, then do the assignment and increment your count. Keep going until you've added the required number of nan runs. It's not hard. Give that a try. You might try isnan() to get a list of what elements are nan.

Sign in to comment.