Suppose I have a vector of eliments including empty spaces in random places. How can I get the average of values that are available.
Example is as follows. Note that there is no value after 1 and 8. So I want to avoid those and get the average of the rest. Here I cannot just use the 'mean(x,1)' because empty indexes. Assume that I am considering a large vector and i cannot find the indices where there are no values, but just empty spaces.
X=[ 2 4 1 5 2 2 7 8 1 3]

답변 (1개)

madhan ravi
madhan ravi 2019년 4월 30일
편집: madhan ravi 2019년 4월 30일

0 개 추천

X={ 2 4 1 [] 5 2 2 7 8 [] 1 3};
idx=cellfun('isempty',X);
X(idx)={NaN};
Wanted=mean([X{:}],'omitnan')
%or
Wanted=mean([X{~idx}])

댓글 수: 3

Thanks. But I was looking for a one line answer. Earlier I remember using the backslash ("\" ) symbol to get rid of unwanted. Whether a one line coding to get the average of values in the vector is possible, if i know the indices of missing data?
This is the one line method
Wanted=mean([X{:}],'omitnan')
madhan ravi
madhan ravi 2019년 4월 30일
That's exactly what was posted.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

태그

댓글:

2019년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by