Estimation of average angles based on quadrants

6 ビュー (過去 30 日間)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2024 年 8 月 25 日
Hello, I would appreaciate any suggestion to estimate the total average of angles based on the location of the quadrants. The length of the matrix of the angles to assess the average is variable and for these examples I am assuming that the length of the matrices are constant:
angles=[2;355;0;6]
I should have the following answer for the average:
average_angle=0.75;
% Another case:
angles=[-2;350;0;-3 ]
average=356.25;
% another case
angles=[5;36;45;-5]
average=110.25
% another case
angles=[35;36;45;50]
average=41.5
% another case
angles=[180;150;-8;130]
average=203
I would appreciate the help if there is a quick command in Matlab to do these operations without having too many conditions.

採用された回答

Umar
Umar 2024 年 8 月 25 日

Hi @Jorge Luis Paredes Estacio,

Your most important task in my opinion is to compute the average of a set of angles, taking into account their positions in the circular coordinate system. The average should be calculated in such a way that it correctly reflects the circular nature of angles, avoiding the pitfalls of simple arithmetic averaging which can lead to incorrect results when angles wrap around (e.g., 359 degrees and 1 degree).

function average_angle = calculate_average_angle(angles)
  % Convert angles to radians
  angles_rad = deg2rad(angles);
    % Calculate the average of the sine and cosine components
    avg_sin = mean(sin(angles_rad));
    avg_cos = mean(cos(angles_rad));
    % Calculate the average angle in radians
    average_rad = atan2(avg_sin, avg_cos);
    % Convert the average angle back to degrees
    average_angle = rad2deg(average_rad);
    % Normalize the angle to be within the range [0, 360)
    if average_angle < 0
        average_angle = average_angle + 360;
    end
  end
% Example cases
angles1 = [2; 355; 0; 6];
average1 = calculate_average_angle(angles1);
disp(['Average Angle 1: ', num2str(average1)]);
angles2 = [-2; 350; 0; -3];
average2 = calculate_average_angle(angles2);
disp(['Average Angle 2: ', num2str(average2)]);
angles3 = [5; 36; 45; -5];
average3 = calculate_average_angle(angles3);
disp(['Average Angle 3: ', num2str(average3)]);
angles4 = [35; 36; 45; 50];
average4 = calculate_average_angle(angles4);
disp(['Average Angle 4: ', num2str(average4)]);
angles5 = [180; 150; -8; 130];
average5 = calculate_average_angle(angles5);
disp(['Average Angle 5: ', num2str(average5)]);

So, after analyzing this code, you will find out that the angles are first converted from degrees to radians using the deg2rad function which is necessary because trigonometric functions in MATLAB operate in radians. Then, the function calculates the average of the sine and cosine of the angles and this step is crucial as it allows you to account for the circular nature of your angles. Please note that the average sine and cosine values represent the coordinates of the average angle on the unit circle then I used atan2 function to compute the angle from the average sine and cosine values which returns the angle in radians, then converted back to degrees using rad2deg. Finally, the average angle is normalized to ensure it falls within the range of [0, 360) degrees. If the calculated average angle is negative, 360 degrees is added to bring it into the desired range. I tested the function is tested with several sets of your angles, and the results are displayed using the disp function. Please see attached.

Please let me know if you have any further questions.

  3 件のコメント
Walter Roberson
Walter Roberson 2024 年 8 月 25 日
angles3 = [5; 36; 45; -5]
angles3 = 4x1
5 36 45 -5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean(mod(angles3,360))
ans = 110.2500
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2024 年 8 月 25 日
Thank you very much. You gave me an idea how to implement the function provided based on different scenarios that includes that specific case. :).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by