How to get an average of two cells in specific conditions?
Show older comments
Hello,
I'm working with a rather large Excel document that I'm importing into Matlab 2016b (or 2019b as well) and trying to manipulate, the data is degrees of a circle as an array of 960x13.
For example:
x = [ 23.50128 22.0385 18.058
35.71267 25.44106 13.09361
9.335854 218.1108 -0.63557
40.69297 27.41063 -1.69481
27.22272 25.38112 -0.57529 ]
I need to average any number within the array that is greater than 90, or less than -90, with the number above and below it then replace the value that exceeds those parameters. In this example, I need 218.1108 to be instead be replaced with the averave of 25.44106 and 27.41063, the rest being left alone. I'm sure I'll have to use some sort of If statement to specify it but I'm uncertain on how to really get at this. Additionally, I run into the problem that sometimes the number outside of the parameters will be directly followed by another number outside the parameters and should in this case not be averaged with it but with the two numbers directly proceeding or following it. This may be rather complicated, but if anyone has any advice on how to approach this it would be greatly appreciated!
Thank you!
Accepted Answer
More Answers (1)
David Hill
on 2 Jan 2020
You could try something like:
a=find(x>90|x<-90);
for i=1:length(a)
x(a(i))=mean(x([a(i)-1,a(i)+1]));
end
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!