finding consecutive NaN in matrix
    16 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    prashant singh
 il 9 Ott 2017
  
    
    
    
    
    Commentato: Image Analyst
      
      
 il 18 Ott 2018
            i have a vector like [1,2,3,4,NaN,NaN,NaN,7,8,9]. I want to find if there is 3 consecutive NaN present in the vector. i can go for loop but is there more direct and easy way to do that.
0 Commenti
Risposta accettata
  Cedric
      
      
 il 9 Ott 2017
        
      Modificato: Cedric
      
      
 il 9 Ott 2017
  
       >> x = [1,2,3,4,NaN,NaN,NaN,7,8,9] ;
 >> strfind( isnan(x), true(1,3) )
 ans =
     5
if you just need a "flag found", do it as follows:
 >> found = ~isempty(strfind( isnan(x), true(1,3) ))
 found =
  logical
   1
2 Commenti
  Tumelo Maja
 il 17 Ott 2018
				How would i apply this method to matrix? I want to find consecutive NaNs in columns of a matrix.
  Image Analyst
      
      
 il 18 Ott 2018
				Apply it one column at a time
for k = 1 : size(m, 2)
    thisColumn = m(:, k);
    nanRows = isnan(thisColumn));
    % Now do whatever you want to do with nanRows.
end
Più risposte (1)
  Image Analyst
      
      
 il 9 Ott 2017
        For a more general purpose function, use isnan() and regionprops() (in the Image Processing Toolbox). It will find all groupings of Nans no matter how long or short they are. You can also specify a size range for the groups if you want, like ignore nans that are only one element long or whatever. If you want this general purpose code, let me know. Otherwise Cedric's code using the handy but little known strfind() trick is great for your given example of 3 nans in a row.
2 Commenti
  Cedric
      
      
 il 9 Ott 2017
				To be honest, I would not have thought about using STRFIND on numbers had I not seen it first on Loren's blog back then.
  Image Analyst
      
      
 il 10 Ott 2017
				Right, I mean who would have thought that a string function could work on numbers? It's not intuitive (that's why I called it a trick).
Anyway, in case anyone is interested, here is the general case that finds lengths and indexes for all the NaN regions in the vector:
m = [1,2,3,4,NaN,NaN,NaN,7,8,9,NaN,NaN,0,NaN,9,NaN,NaN,NaN,NaN] % Sample data
nanLocations = isnan(m) % Get logical array of whether element is NaN or not.
props = regionprops(nanLocations, 'Area', 'PixelIdxList'); % Find all the regions.
% DONE!  Now let's print them out
for k = 1 : length(props)
  fprintf('Region #%d has length %d and starts at element %d and ends at element %d\n',...
    k, props(k).Area, props(k).PixelIdxList(1), props(k).PixelIdxList(end));
end
Here's what shows up in the command window:
m =
     1     2     3     4   NaN   NaN   NaN     7     8     9   NaN   NaN     0   NaN     9   NaN   NaN   NaN   NaN
nanLocations =
  1×19 logical array
   0   0   0   0   1   1   1   0   0   0   1   1   0   1   0   1   1   1   1
Region #1 has length 3 and starts at element 5 and ends at element 7
Region #2 has length 2 and starts at element 11 and ends at element 12
Region #3 has length 1 and starts at element 14 and ends at element 14
Region #4 has length 4 and starts at element 16 and ends at element 19
Vedere anche
Categorie
				Scopri di più su Data Type Conversion in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!