How can I determine the indices and length of consecutive non-NaN values in an array?
    14 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Jonathan Macuroy
 am 28 Jun. 2019
  
    
    
    
    
    Kommentiert: Stephen23
      
      
 am 29 Jun. 2019
            Say I have a vector A = [NaN NaN NaN 10 22 NaN 33 28 21 NaN 20 98 NaN], how do I:
1) determine the indices of consecutive non-NaN values; and
2) extract the count and length of these 'consecutive events'
Thank you!
0 Kommentare
Akzeptierte Antwort
  Stephan
      
      
 am 28 Jun. 2019
        
      Bearbeitet: Stephan
      
      
 am 28 Jun. 2019
  
      1. FInd the indexes:
B = find(isnan(A))
2. To find consecutive blocks you could use the diff function on a logical array:
C = isnan(A) % gives a logical array
diff(C)      % --> is different from zero, when a block changes (1 for start and -1 for end of block)
2 Kommentare
  Stephen23
      
      
 am 29 Jun. 2019
				Note that the code Stephan shows to "find consecutive blocks" will not detect when the non-NaN blocks occur at the ends of the vector.
Weitere Antworten (1)
  Stephen23
      
      
 am 28 Jun. 2019
        
      Bearbeitet: Stephen23
      
      
 am 28 Jun. 2019
  
      1)
>> A = [NaN NaN NaN 10 22 NaN 33 28 21 NaN 20 98 NaN];
>> find(~isnan(A))
ans =
    4    5    7    8    9   11   12
or do you mean something else by "consecutive non-NaN values" ? Do you only want to get the indices at the beginning+end of each run of non-NaNs ?
2)
>> D = diff([false,~isnan(A),false]);
>> L = find(D<0)-find(D>0) % length
L =
   2   3   2
>> N = numel(L) % count
N =  3
Siehe auch
Kategorien
				Mehr zu Logical finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


