Filter löschen
Filter löschen

eliminate indices that are empty

1 Ansicht (letzte 30 Tage)
Thishan Dharshana Karandana Gamalathge
Kommentiert: madhan ravi am 30 Apr. 2019
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]

Antworten (1)

madhan ravi
madhan ravi am 30 Apr. 2019
Bearbeitet: madhan ravi am 30 Apr. 2019
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 Kommentare
madhan ravi
madhan ravi am 30 Apr. 2019
This is the one line method
Wanted=mean([X{:}],'omitnan')
madhan ravi
madhan ravi am 30 Apr. 2019
That's exactly what was posted.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by