Group data based on direct
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ancalagon8
am 8 Jan. 2023
Bearbeitet: Ancalagon8
am 6 Jan. 2025
I have a table with values and I want to group them in sectors. Is it possible to do it automatically either than manually?
4 Kommentare
Adam Danz
am 9 Jan. 2023
Bearbeitet: Adam Danz
am 9 Jan. 2023
Grouping angular values
This demo works with degrees but it's the same process for radians. It assumes angular data are between 0 and 360. If that's not the case in your data, wrap your data to 360 (or to 2pi).
Given the number of bins nBins this computes equispaced bin edges, with a bin centered at 0 deg, and then computes the bin number for each angular data point. The bin centered at 0 is delt with separately.
Grouping isn't too difficult. However, depending on what you plan to do with these group values, you could run into trouble that @Walter Roberson explained. Phase wrapping is a potential solution to this problem (I'm looking at you bin #1).
theta = [rand(1,30)*360]; % vector of angular data [0,360] deg
nBins = 12; % select number of bins
% Compute edges
w = 360/nBins;
edges = w/2:w:(360-w/2);
% Bin data
bin = discretize(theta, edges)+1;
bin(theta>=edges(end) | theta<edges(1)) = 1; % bin at 0
% Plot results, show theta values, bin edges, and bin ID
polaraxes('ThetaZeroLocation','top',...
'RTickLabel','', ...
'ThetaDir','ClockWise')
hold on
polarplot(theta*pi/180, 1, 'ro')
polarplot([1;1].*edges*pi/180, [0;1], 'k-')
text(theta*pi/180, ones(size(theta))-.1, string(bin), ...
'HorizontalAlignment','Center', 'VerticalAlignment', 'middle')
grid off
Walter Roberson
am 9 Jan. 2023
See also the discussion at https://www.mathworks.com/matlabcentral/answers/128381-how-to-calculate-mean-wind-direction
Akzeptierte Antwort
Star Strider
am 8 Jan. 2023
Bearbeitet: Star Strider
am 8 Jan. 2023
WindDir = rand(100,1)*360;
edgev = linspace(0, 360, 9);
[N,Edges,Octant] = histcounts(WindDir, edgev);
WindOctants = table(WindDir,Octant)
The first output are the number of counts in each octant (bonus information), and the third is the respective octant.
EDIT — (8 Jan 2023 at 16:30)
Added table.
.
11 Kommentare
Star Strider
am 23 Feb. 2023
I thought I could do this with one of the table functions, however extracting and transposing proved to be easiest —
LD = load(websave('T','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289030/T.mat'))
% DT = datetime('01-Jan-2022') + caldays(0:365).';
% WindVel = lognrnd(log(5),log(3),size(DT));
TT = LD.T
% return
MM = month(TT.date_time);
T = timetable2table(TT);
Vmax = ceil(max(TT.Vel_avg)) % Maximum Velocity In File
edgev = linspace(0, Vmax, Vmax+1); % Edge Vector
edgem = [edgev(1:end-1); edgev(2:end)].'; % Bin Limits (For Output Table)
ctrs = mean(diff(edgev))/2 + edgev(1:end-1); % Bin Centers
[N,Edges,mps] = histcounts(TT.Vel_avg, edgev); % Call 'histcounts'
figure
bar(ctrs, N)
grid
xlabel('Velocity')
ylabel('Counts')
WindVelCounts = table(edgem(:,1),edgem(:,2)); % Initialise Table
for k = 1:12
Month = T(MM==k,:);
[N,Edges,mps] = histcounts(T.Vel_avg(MM==k,:), edgev); % Call 'histcounts¹
Nc{k} = N;
Dch{k+2} = mps;
WindVelCounts{:,k+2} = N(:);
MMM{k} = month(Month{1,1},'shortname');
end
MMMv = [MMM{:}];
WindVelCounts.Properties.VariableNames = {'Start','End', MMMv{:}}
VN = WindVelCounts.Properties.VariableNames;
WindVelCountsTransposed = array2table(table2array(WindVelCounts(:,3:end)).', 'RowNames',VN(3:end));
WindVelCountsTransposed.Properties.VariableNames = compose('[%d-%d]',edgem)
% figure
% TL = tiledlayout(4,3);
% for k = 1:12
% nexttile
% bar(ctrs,WindVelCounts{:,k+2})
% grid
% ylim([0 1.8E+4])
% title(MMM{k})
% end
% title(TL,year(TT.date_time(1,1)))
All the original information is still there in case you need it. Plotting it using bar3 might be another option, in addition to tiledlayout.
.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Geographic Plots 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!