eliminate indices that are empty
Mostrar comentarios más antiguos
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]
Respuestas (1)
madhan ravi
el 30 de Abr. de 2019
Editada: madhan ravi
el 30 de Abr. de 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 comentarios
Thishan Dharshana Karandana Gamalathge
el 30 de Abr. de 2019
madhan ravi
el 30 de Abr. de 2019
madhan ravi
el 30 de Abr. de 2019
That's exactly what was posted.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!