replacing the NaN with specific values
    2 views (last 30 days)
  
       Show older comments
    
    tzaloupas
 on 14 Mar 2013
  
    
    
    
    
    Commented: Khaing Zin Htwe
 on 10 May 2016
            Dear all,
I have the following matrix
   A={'country'  'values'
       'E'    '1'  
    'E'    [NaN]
    'E'    [NaN]
    'E'    '2'  
    'E'    [NaN]
    'E'    [NaN]
    'E'    [NaN]
    'E'    [NaN]
    'E'    [NaN]
    'E'    '3'  
    'E'    [NaN]
    'E'    [NaN]
    'E'    [NaN]
    'E'    [NaN]
    'EE'    [NaN]
    'E'    '4'
     'I'    '1'  
    'I'    [NaN]
    'I'    [NaN]
    'I'    '2'  
    'I'    [NaN]
    'I'   [NaN]
    'I'    [NaN]
    'I'    [NaN]
    'I'    [NaN]
    'I'    '3'  
    'I'    [NaN]
    'I'    [NaN]
    'I'    [NaN]
    'I'   [NaN]
    'I'    [NaN]
    'I'    '4'
    'I'    [NaN]
    'I'    [NaN]
    'I'    [NaN]
    'I'   [NaN]
    'I'    [NaN]
    'I'    '5'
     'K'    '1'  
    'K'    [NaN]
    'K'    [NaN]
    'K'     '2'  
    'K'     [NaN]
    'K'    [NaN]
    'K'     [NaN]
    'K'     [NaN]
    'K'     [NaN]
    'K'    [NaN]   
   'K'    [NaN]
    'K'    '3'
    'K'     [NaN]
    'K'    [NaN]
    'K'    [NaN]
    'K'     '4'
    'K'    [NaN]
    'K'     [NaN]
    'K'     [NaN]
    'K'    [NaN]
    'K'     [NaN]
    'K'     '5'
};
The first column describes the countries and the second the values corresponding to each country.
For each country the initial value is always '1'. For each country I want to replace the NaNs with the value that is before them.
So for the first country I want to have
'E'    '1'  
  'E'   '1' 
  'E'    '1' 
  'E'    '2'  
  'E'   '2' 
  'E'    '2' 
  'E'    '2' 
  'E'    '2' 
  'E'    '2' 
  'E'    '3'  
  'E'    '3' 
  'E'    '3' 
  'E'    '3' 
  'E'    '3' 
  'EE'    '3' 
  'E'    '4'
    for country I I want to have
        'I'    '1'  
    'I'    '1' 
    'I'   '1' 
    'I'    '2'  
    'I'   '2' 
    'I'  '2' 
    'I'    '2' 
    'I'   '2' 
    'I'    '2' 
    'I'    '3'  
    'I'   '3'  
    'I'    '3'  
    'I'    '3'  
    'I'   '3'  
    'I'    '3'  
    'I'    '4'
    'I'    '4'
    'I'   '4'
    'I'    '4'
    'I'   '4'
    'I'   '4'
    'I'    '5'
    and so so..
    I am trying to come up with a code but I got stuck
    Is there any way of doing that
    My real vector contains 300 countries, so doing manual is quite tedious
    thanks in advance
0 Comments
Accepted Answer
  Andrei Bobrov
      
      
 on 14 Mar 2013
        
      Edited: Andrei Bobrov
      
      
 on 14 Mar 2013
  
      A1 = A(2:end,:);
t = ~cellfun(@isnan,A1(:,2));
k = A1(t,2);
A(2:end,2) = k(cumsum(t));
added on comment
A1 = A(2:end,2);
t = cellfun(@ischar,A1);
A1(t) = cellfun(@str2double,A1(t),'un',0);
t1 = ~cellfun(@isnan,A1);
k = A1(t1);
A(2:end,2) = k(cumsum(t1));
5 Comments
  Khaing Zin Htwe
 on 10 May 2016
				To replace integer zeros to NaN vaules in Array ,how can I do it ,sir.I have 141x3084 array that include NaN values unfortunately. After reducing dimensions to give input to ANFIS classifier, i get the result as NaN,seriously. Please help me ,sir , Andrei Bobrov.
More Answers (0)
See Also
Categories
				Find more on Resizing and Reshaping Matrices 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!

