Computation of mean and standard deviation after supressing NaNs of an array

조회 수: 2 (최근 30일)
Naga
Naga 2015년 4월 8일
댓글: Greg Dionne 2015년 4월 9일
nnmax = 4;
for nn=2:nnmax
D_filt(nn,:) = D_filt(nn-1,:);
[Dmax(nn,aa),Imax(nn,aa)] = max(D_filt(nn-1,:));
D_filt(nn,Imax(nn,aa)) = NaN ;
end
#Comments (loop indicates)
1. Scans from 0 to 360 degrees with 10 degree interval.
2. aa represents the same [0:10:360]
3. At each value of angle, the detector collects 10 data points
4. OBJECTIVE: To discard 4 data points out of the 10 and compute mean and SD
5. nn represnts the iteration number while supressing the bad data points
6. The output of the above loop displays
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 0 0
1 2 3 4 5 6 7 0 0 0
Instead of 4 rows, a single row matrix after supressing four bad points is desired, so that mean and SD can be computed with ease.

답변 (2개)

Thomas Koelen
Thomas Koelen 2015년 4월 8일
편집: Thomas Koelen 2015년 4월 8일
Hi Naga,
The term "average" usually encompasses several ways to measure what value best represents a sample. There are various measurements that are used and at times the term and measurement used depends on the situation.
A statistician or mathematician would use the terms mean and average to refer to the sum of all values divided by the total number of values, what you have called the average. This especially true if you have a list of numbers. In fact even in mathematics there are different "averages" or "means" and this one is more properly called the arithmetic mean.
I think the thing you are trying to accomplish is straight up using the function:
mean()
For the standard deviation use this function:
std()
Thomas Koelen
  댓글 수: 3
Thomas Koelen
Thomas Koelen 2015년 4월 8일
편집: Thomas Koelen 2015년 4월 8일
a(isnan(a(:,1)),:)=[]
this should give you your vector without NaN, then you can just apply the mean() and the std() function.
have a look at this, a is your vector here.
Naga
Naga 2015년 4월 8일
편집: Naga 2015년 4월 8일
When I run the program, the output is zeroes instead of NaN, so when I use your above function, there's no change in the output.
Just to give you an idea, When I run the above loop the following is being displayed in the output
D_filt
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 0 0
1 2 3 4 5 6 7 0 0 0
So, there are no NaNs in principle

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


Greg Dionne
Greg Dionne 2015년 4월 8일
편집: Greg Dionne 2015년 4월 8일
You can use mean(x,'omitnan') in R2015a. If you have the Statistics Toolbox, you can use nanmean.
Otherwise, try this:
n = sum(~isnan(x));
x(isnan(x)) = 0;
s = sum(x);
avg = s./n;
  댓글 수: 7
Naga
Naga 2015년 4월 9일
Actually, I cited you an example here. But, in reality the zeros are not at the end always, they can be at any position depending on the occurence of the 'NaN'.
Greg Dionne
Greg Dionne 2015년 4월 9일
In that case you could do:
sum(x(4,:),2) ./ sum(0~=x(4,:),2)
But it seems you are still confused as to why you are not getting NaN in your output like you are expecting. Perhaps if you posted your data (save it to a MAT file and post it to your question) and a quick blurb about what you want to see happen (i.e. what you expect the answer to be) we'll be able to point you in the right direction.
Good luck!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by