# Filter data based on wind direction

6 views (last 30 days)
Michiel Smit on 22 Mar 2022
Answered: Michiel Smit on 22 Mar 2022
Currently I am working with a dataset consisting of windspeeds (v) and directions (phi) in several locations. I need to filter the dataset based on wind direction. That is, I have determined the dominant wind directions at each of the locations and need to remove all the data outside dominant direction +/- 30 degrees. This is of course no problem when the dominant wave direction is anywhere between 30 and 330 degrees. However, when the dominant direction is less than 30 degrees or more than 330 degrees, it becomes problematic since 360 degrees is equal to 0 degrees (North).
Let's say I have:
v = rand(1000,1)*15 % Wind speed between 0 and 15 m/s
phi = rand(1000,1)*360 % Wind direction between 0 and 360 degrees North
It should not be difficult, but I cannot figure it out. What is an easy solution to filter the dataset based on the wind direction so that it accounts for the fact that the wind direction is in fact circular data?

Michiel Smit on 22 Mar 2022
Nevermind, I managed to find a solution myself. It is not very elegant, but at least it works:
v = rand(1000,1)*15;
phi = rand(1000,1)*360;
Data = [v,phi];
theta = 30;
theta_bounds = [theta-45,theta+45];
if theta_bounds(1) < 0; % For thetas just above 0
L_bound = rem(360+theta_bounds(1),360);
U_bound = rem(360+theta_bounds(2),360);
% Data can be cut based on an OR statement
idx = find(Data(:,2) >= L_bound | Data(:,2) <= U_bound);
Data_cut = Data(idx,:);
elseif theta_bounds(2) > 360; % For thetas just below 360
L_bound = rem(theta_bounds(1),360);
U_bound = rem(theta_bounds(2),360);
% Data can be cut based on an OR statement
idx = find(Data(:,2) >= L_bound | Data(:,2) <= U_bound);
Data_cut = Data(idx,:);
else
% For theta anywhere between 330
L_bound = rem(theta_bounds(1),360);
U_bound = rem(theta_bounds(2),360);
% Data can be cut based on an AND statement
idx = find(Data(:,2) >= L_bound & Data(:,2) <= U_bound);
Data_cut = Data(idx,:);
end
figure()
scatter(Data(:,2),Data(:,1),10,'filled')
hold on
scatter(Data_cut(:,2),Data_cut(:,1),10,'filled')
xlim([0,360])

### Categories

Find more on Logical in Help Center and File Exchange

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!