Search code examples
matlabinterpolationdata-analysis

Make a vector equal to another by filling 'NaN' without interpolation


I have a time stamp as follow.

Time =

  243.0000
  243.0069
  243.0139
  243.0208
  243.0278
  243.0347
  243.0417
  243.0486
  243.0556
  243.0625
  243.0694
  243.0764
  243.0833
  243.0903
  243.0972
  243.1042
  243.1111
  243.1181
  243.1250
  243.1319
  243.1389
  243.1458
  243.1528
  243.1597
  243.1667
  243.1736
  243.1806
  243.1875
  243.1944    

Now I have another two column vector.

ab =

 243.0300    0.5814
 243.0717    0.6405
 243.1134    0.6000
 243.1550    0.5848
 243.1967    0.5869

First column is 'Time2' and second column is 'Conc'.

 Time2 = ab(:,1);
 Conc  = ab(:,2);

Now I want to match 'Conc' based on 'Time2' with 'Time' but only filling with 'NaN'. Also 'Time2' is not exactly as 'Time'. I can use something like following

Conc_interpolated = interp1(Time2,Conc,Time)

but it does an interpolation with artificial data. I only want to match vector length by filling with 'NaN' in 'Conc' not with interpolated data.Any recommendations? Thanks


Solution

  • My understanding is a little different, it doesn't add to Time but rather assigns each Conc to the nearst Time based on it's Time2:

     ind = zeros(size(ab,1),1); %//preallocate memory
     for ii = 1:size(ab,1)
        [~, ind(ii)] = min(abs(ab(ii,1)-Time)); %//Based on this FEX entry: http://www.mathworks.com/matlabcentral/fileexchange/30029-findnearest-algorithm/content/findNearest.m
     end
    
     Time(:,2) = NaN; %// Prefill with NaN
     Time(ind, 2) = ab(:,2)
    

    This results in:

    Time =
    
       243.00000         NaN
       243.00690         NaN
       243.01390         NaN
       243.02080         NaN
       243.02780     0.58140
       243.03470         NaN
       243.04170         NaN
       243.04860         NaN
       243.05560         NaN
       243.06250         NaN
       243.06940     0.64050
       243.07640         NaN
       243.08330         NaN
       243.09030         NaN
       243.09720         NaN
       243.10420         NaN
       243.11110     0.60000
       243.11810         NaN
       243.12500         NaN
       243.13190         NaN
       243.13890         NaN
       243.14580         NaN
       243.15280     0.58480
       243.15970         NaN
       243.16670         NaN
       243.17360         NaN
       243.18060         NaN
       243.18750         NaN
       243.19440     0.58690
    

    for your example inputs