user2126062
user2126062

Reputation: 121

Matlab - find a missing time from a matrix and insert the missing time with a value

I have a series of times and returns in various matrices lets call them a b c. They are all x by 2 with column 1 being times in seconds and column 2 returns. While all the returns are over a set series of time intervals like 15s 30s 45s etc the problem is not all the matrices have all the time buckets so while a might be a 30 by 2 , b might only be a 28 by 2. Because it is missing say time 45 seconds and a return. I want to go through each matrix and where I am missing a time bucket I want to insert the bucket with a zero return - I am happy to create a control 30 by 1 matrix with all the times that need to be cross referenced

Upvotes: 2

Views: 436

Answers (1)

bla
bla

Reputation: 26069

You can use ismember to locate these missing positions, so if a is the control vector and b is the missing data vector ind=find(ismember(a,b)==0); will give you the indices of a that are missing in b.

For example:

a=1:10;
b=[1:2 4:5 7:10];
ind=find(ismember(a,b)==0);

ind =
     3     6

In order to to add zeros in the right places for b just

for n=1:numel(ind)
    b=[b(1:ind(n)-1) , 0 , b(ind(n):end)];
end

b =
 1     2     0     4     5     0     7     8     9    10

Upvotes: 1

Related Questions