how to calculate mean of interrupted data
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
osasunmwen efosa
am 11 Mai 2023
Kommentiert: osasunmwen efosa
am 3 Jun. 2023
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 Kommentare
Dyuman Joshi
am 12 Mai 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?
Akzeptierte Antwort
Dyuman Joshi
am 12 Mai 2023
A=1:20;
A([2 4 8 16]) = NaN;
disp(A)
idx = [0 find(isnan(A)) numel(A)+1]
for k = 1:numel(idx)-1
%Range of indices between starting point, NaNs and ending point
arr=idx(k)+1:idx(k+1)-1;
A(arr) = A(arr) - mean(A(arr));
end
disp(A)
4 Kommentare
Dyuman Joshi
am 3 Jun. 2023
"it turns out that the standard deviation of the result is different from the standard deviation of the original dataset."
Yes, that is expected as we are manipulating the data.
"Is there a way to preserve the standard deviation of the orignal data ?"
You can preserve the standard deviation of the original data, but that will result in a different output.
Weitere Antworten (1)
Antoni Garcia-Herreros
am 11 Mai 2023
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
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!