Replacing value by the mean of the two nearest neighbors
20 views (last 30 days)
Show older comments
Ludovica Maria Campagna
on 26 Sep 2022
Commented: Alexis
on 24 Jan 2024
I have a column vector:
B = [20; 18; NaN; 25; 100; 15; -50; 23]
I need to pre-process data, so I want to:
- replace numbers greater than a certain value (e.g. numbers greater than 40) with the average of the two adjacent numbers.
- replace numbers smaller than a certain value (e.g. numbers smaller than -10) with the average of the two adjacent numbers.
- replace missing value with the average of the two adjacent numbers.
Thank you!
2 Comments
Torsten
on 26 Sep 2022
The result will depend on the order in which you perform these operations.
So order doesn't matter ?
Accepted Answer
David Hill
on 26 Sep 2022
B = [20; 18; NaN; 25; 100; 15; -50; 23; NaN; 15; -15; 30];
idx=find(isnan(B));
B(idx)=mean([B(idx-1),B(idx+1)],2);
idx=find(B<-10);
B(idx)=mean([B(idx-1),B(idx+1)],2);
idx=find(B>40);
B(idx)=mean([B(idx-1),B(idx+1)],2)
1 Comment
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!