Moving mean of a vector with unsorted values
4 views (last 30 days)
Show older comments
Hello everyone,
I have a vector of measured angle values (1x61 double), which was measured in this direction: 15:-1:-15 and then in the reverse direction as -14.5:1:14.5 degree. So the resulting vector is [15 14 13 ... -13 -14 -15 -14.5 -13.5 -12.5 ... 12.5 13.5 14.5]. Then I have corresponding measured Cd values for the angles, also 1x61 double.
I tried to do a moving average
Cdormov = movmean(Cd_original, 5);
When I plot the results, I can see that Matlab calculated the moving average for the Cd values as for two vectors. However, I need Matlab to treat the angle and Cd data as one X and one Y vector and calculate only one moving mean curve. How can I do this? Thank you.
The plot code:
figure(4)
hold on; grid on; grid minor;
plot(angle,Cd_original,'black-+');
plot(angle,Cdormov,'red-o');
xlabel('angle (deg)'); ylabel('Cd (-)');
legend('original data','moving mean');
title(['Cd ' name])
The plot:

0 Comments
Accepted Answer
Voss
on 20 Mar 2025
% specified angle vector
angle = [15:-1:-15 -14.5:1:14.5];
% example Cd_original vector
Na = numel(angle);
Cd_original = sin(angle/4)+0.5*round((0:Na-1)/Na);
% sort angle, and then calculate the moving mean on
% Cd_original reordered according to angle_sorted
[angle_sorted,idx] = sort(angle);
Cdormov = movmean(Cd_original(idx), 5);
% when you plot, plot Cdormov against angle_sorted
name = 'V_o21_23proc';
figure(4)
hold on; grid on; grid minor;
plot(angle,Cd_original,'black-+');
plot(angle_sorted,Cdormov,'red-o')
xlabel('angle (deg)'); ylabel('Cd (-)');
legend('original data','moving mean');
title(['Cd ' name])
1 Comment
Voss
on 20 Mar 2025
Compare the results of unique-ing vs merely sorting, in a case with lots of duplicate values in the angle vector:
% angle vector
angle = [5:-0.5:-5 -4.5:0.5:4.5];
% lots of repeated values
numel(angle), numel(unique(angle))
% example Cd_original vector
Na = numel(angle);
Cd_original = sin(angle)+0.5*round((0:Na-1)/Na);
% use unique and sort and see what happens
[angle_unique,idx_unique] = unique(angle);
Cdormov_unique = movmean(Cd_original(idx_unique), 5);
[angle_sort,idx_sort] = sort(angle);
Cdormov_sort = movmean(Cd_original(idx_sort), 5);
figure(4)
tiledlayout(2,1)
nexttile()
hold on; grid on; grid minor;
plot(angle,Cd_original,'black-+');
plot(angle_unique,Cdormov_unique,'red-o')
xlabel('angle (deg)'); ylabel('Cd (-)');
legend('original data','moving mean');
title('with unique()')
nexttile()
hold on; grid on; grid minor;
plot(angle,Cd_original,'black-+');
plot(angle_sort,Cdormov_sort,'red-o')
xlabel('angle (deg)'); ylabel('Cd (-)');
legend('original data','moving mean');
title('with sort()')
More Answers (1)
Stephen23
on 20 Mar 2025
You will need to interlace your data points before calling MOVMEAN. For example:
xL = +15.0:-1:-15.0;
xR = -14.5:+1:+14.5;
yL = sin(xL/4);
yR = sin(xR/4)+0.5;
yU = [yL,yR];
[xU,idx] = unique([xL,xR]);
yU = yU(idx);
yM = movmean(yU,5);
plot(xL,yL,'-+k', xR,yR,'-xk', xU,yM,'-*r')
0 Comments
See Also
Categories
Find more on Beamforming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!