how to calculate mean of interrupted data
Mostrar comentarios más antiguos
how can one program matlab to calculate the zero mean of a time series but only for values before a NaN value and then values after a NaN value. i am not talking about the omitnan function.
4 comentarios
Dyuman Joshi
el 11 de Mayo de 2023
%Indices of NaN values
idx = find(isnan(data))
%indices of values before and after NaN, and removing indices overlapping
%that of NaN values, in case there are consecutive NaN values
k=setdiff([idx-1 idx+1],idx)
out = data(k);
%zero mean
out = out-mean(out)
osasunmwen efosa
el 11 de Mayo de 2023
I am not sure if I understand what you want to achieve.
Let's assume this to be your data -
A=1:20;
A([2 4 8 16]) = NaN
What should be the output for this?
osasunmwen efosa
el 12 de Mayo de 2023
Editada: osasunmwen efosa
el 12 de Mayo de 2023
Respuesta aceptada
Más respuestas (1)
Hello,
You could try something like this:
A=[1:10]; % For the example
A(5)=NaN; A(8)=NaN
R=[1 find(isnan(A)) length(A)]; % Indices where the nans are + the beggining and end of vector
MeanVec=zeros(size(R,2)-1,1); % Initialize vector where the means will be stored
for i=1:length(R)-1 % Loop through the different sections between nans
F(i)=nanmean(A(R(i):R(i+1)));
end
F
1 comentario
osasunmwen efosa
el 12 de Mayo de 2023
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!