Averaging sections of a given column

조회 수: 1 (최근 30일)
Thishan Dharshana Karandana Gamalathge
편집: Image Analyst 2017년 7월 28일
Suppose I have a column vector A=[1 3 -999 4 9 8 -999 3 5]; I need to get average of each 3 sections. The problem is, if any section contains -999, I need to avoid it and get the average of rest of the numbers. For an example, for the 1st three numbers, average has to be (1+3)/2.
Please help me with that. Thanks.
  댓글 수: 3
Thishan Dharshana Karandana Gamalathge
Thanks. But using this code, if there were three -999 somewhere in the middle, that section will be replaced by the avg of next three. At the same time, if there are three -999 at the end, it will be not shown at all. In those cases, let's put NaN so that I know which sections had three consecutive -999. Consider the following to help me with coding.
A=[1 3 -999 -999 -999 -999 4 9 8 -999 -999 -999]; The correct answer I am expecting is B=[2 NaN 7 NaN]
Thanks.
Image Analyst
Image Analyst 2017년 7월 28일
편집: Image Analyst 2017년 7월 28일
That seems inadvisable. Why do you want nans in there instead of the very simple approach I showed you below?????
MSP, that should have been in the answer section below since you intended it as an answer rather than a comment (like asking for more information).

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2017년 7월 28일
If you have the Image Processing Toolbox you can use regionprops():
A=[1 3 -999 4 9 8 -999 3 5]
props=regionprops(A~=-999, A, 'MeanIntensity');
sectionMeans = [props.MeanIntensity]
You get:
sectionMeans =
2 7 4

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by