-2

I have a large array with values representing wind direction in degrees(0-360) I am required to calculate the instantaneous rate of change with respect to time, using backwards, forward and then central difference methods.

So far I have managed to calculate the smallest angle difference between elements, but I cant figure out how to also establish a direction, ie, +ve difference for clockwise, -ve for anticlockwise.

for example

winddir =

80
80
70
70 
60
360
330

diff = min((abs(diff(winddir))),(360- abs(diff(winddir))));

diff =

 0
10
 0
10
60
30

This gives the correct values of difference between headings to calculate the derivative, but fails to show direction clockwise and anticlockwise, which I need! I've been trying to figure it out for hours and I'm just going around in circles, could anyone help me out?

Roushan
  • 4,074
  • 3
  • 21
  • 38

1 Answers1

0

First of all, it is very bad practice to use MATLAB's function names as variable names.

Then, in order to calculate your differences the way your want :

myDiff=diff(winddir);
myDiff(myDiff<-180)=myDiff(myDiff<-180)+360;
myDiff(myDiff>180)=myDiff(myDiff>180)-360;
BillBokeey
  • 3,168
  • 14
  • 28
  • Thank you for the advice, this worked perfectly. This is my first attempt at programming and I'm finding its a difficult skill to aquire at first. Thank you again – Dylan Daley Oct 17 '15 at 23:30