Constructing an if statement when a column contains only one data point and the rest are NaNs
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
Dear all,
I have the double array
2.8000    0.2333    0.0010    0.0022
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
I Want to see if each column contains at least two data points using an if statement.
For instance
 for c=1:size(A,2)
  if  A(:,c) contains only one data point
'do that'
end
end
댓글 수: 0
채택된 답변
  per isakson
      
      
 2012년 8월 11일
        
      편집: per isakson
      
      
 2012년 8월 11일
  
      This script prints
Column 2 has it
Column 3 has it
====
   M = [   
       2.8000    0.2333    0.0010    0.0022
       inf       17        17        NaN
       NaN       NaN       18        NaN
       NaN       NaN       NaN       NaN      ];
   has_two_or_more = sum(double(not(isnan(M)|isinf(M))), 1 ) >= 2;
   col = 0;
   for has = has_two_or_more
      col = col + 1;
      if has
          fprintf( 'Column %d has it\n', col ) 
      end    
   end
댓글 수: 3
  per isakson
      
      
 2012년 8월 12일
				That's fine, however you abandon the condition "at least two data points". I prefer to change my loop to.
   for col = 1 : size( M, 2 )
      if has_two_or_more( col )
          fprintf( 'Column %d has it\n', col ) 
      end    
   end
It's about readability, testability and maybe a microsecond. I like to test the condition separately; outside the loop. "Hardcoding" the word "two" in the variable name was silly.
  Image Analyst
      
      
 2012년 8월 12일
				Well like I said in my response, he said "one data point" in the subject and then both "one" and "two data points" in the body of his message. He was unclear so that's why I made mine handle any number. It counts how many there are and then you can decide if you want 1 or 2 or whatever.
추가 답변 (2개)
  Image Analyst
      
      
 2012년 8월 11일
        From your wording it's not clear whether you want to find "two data points" or "only one data point", since you say it both ways.
Try this:
A=[2.8000    0.2333    0.0010    0.0022
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN
     NaN       NaN       NaN       NaN];
   [rows columns] = size(A);
   for c = 1 : columns
     rowsWithNumbers = ~isnan(A(:,c));
     numberOfNumbers = sum(rowsWithNumbers);
     fprintf('Column #%d has %d numbers and %d NaNs\n',...
       c, numberOfNumbers, rows - numberOfNumbers)
  end
Results in command window:
Column #1 has 1 numbers and 5 NaNs
Column #2 has 1 numbers and 5 NaNs
Column #3 has 1 numbers and 5 NaNs
Column #4 has 1 numbers and 5 NaNs
댓글 수: 0
  Azzi Abdelmalek
      
      
 2012년 8월 11일
        
      편집: Azzi Abdelmalek
      
      
 2012년 8월 11일
  
       ind=sum( arrayfun(@(x) ~isnan(x),A))
 for k=1:length(ind)
    if ind(k)>=2
        %do 
    end
 end
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



