What code would I use to find the first zero in a column?
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
I need to calculate the peak power from several sets of data which I have imported into Matlab. This is what the data looks like:

There is a string on data in column 5 which at some point changes to zeros, so what I need to do is find the average of all the numbers until the number changes to zero. How would I tell Matlab to add up and find the peak of the data until it changes to a 0?
댓글 수: 0
채택된 답변
  Jan
      
      
 2014년 4월 22일
        Are you talking about a vector or should this be applied to columns of a matrix?
For a vector:
x = rand(100, 1);
x(17:100) = 0;
idx = find(x == 0, 1);
if ~isempty(idx)
  result = sum(x(1:idx - 1)) / (idx - 1);
else  % No zero found:
  result = sum(x) / length(x);  % Or mean() of course
end
For a matrix you could work with Idx = cumsum(X == 0, 1) == 0. But creating the intermediate index matrix will most likely need more time than the vectorization saves. Therefore I'd stay at the above method for vectors and put it into a loop over the columns.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!