For elements in two arrays/columns of numbers, how can I find the average only if both elements are non-zero?
    조회 수: 2 (최근 30일)
  
       이전 댓글 표시
    
I have many external files.  Each file contains two long columns of numbers, like this:
1        2
11       12
6        48
9        0.0
3        0.5
I can read each file in.  Now I want to calculate the average of each row.  In the column of results, for example, the elements would be: 
(1 + 2 / 2) 
(11 + 12 / 2)
(6 + 48 / 2) ...
The problem is, I don't want to take the average of rows where one element is zero.  For example, I want the row that has
9        0.0
to be "9."  Does anyone know how I can do, "calculate the average of each row, but leave the row alone if one element is zero?"  I'm using 2016a.  Thank you for any advice.
댓글 수: 0
채택된 답변
  madhan ravi
      
      
 2020년 7월 21일
        matrix(matrix == 0) = nan
nanmean(matrix, 2)
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

