down sampling a vector keeping the order of elements values
4 views (last 30 days)
Show older comments
I have a vector of u with size of 45X1.
I want to down sampling it to u_new with size of 7X1. u here is the velocity profile in a tube. So I'd like to have 7 points of them instead of 45 points to plot it with 7 points. when moving from the start and end points of the vector toward the center, data values are increasing. I need to keep this order for 7 bins as well.
what would be the best way to do this?
u=
0.0182
0.0253
0.0324
0.0404
0.0487
0.0552
0.0603
0.0643
0.0674
0.0699
0.0718
0.0732
0.0743
0.0751
0.0757
0.0762
0.0767
0.0770
0.0772
0.0773
0.0774
0.0775
0.0775
0.0775
0.0774
0.0772
0.0770
0.0767
0.0764
0.0760
0.0755
0.0749
0.0740
0.0730
0.0716
0.0699
0.0676
0.0646
0.0608
0.0559
0.0495
0.0416
0.0341
0.0273
0.0199
0 Comments
Answers (1)
Voss
on 17 Jul 2023
u=[
0.0182
0.0253
0.0324
0.0404
0.0487
0.0552
0.0603
0.0643
0.0674
0.0699
0.0718
0.0732
0.0743
0.0751
0.0757
0.0762
0.0767
0.0770
0.0772
0.0773
0.0774
0.0775
0.0775
0.0775
0.0774
0.0772
0.0770
0.0767
0.0764
0.0760
0.0755
0.0749
0.0740
0.0730
0.0716
0.0699
0.0676
0.0646
0.0608
0.0559
0.0495
0.0416
0.0341
0.0273
0.0199
];
x = 1:numel(u);
plot(x,u)
hold on
x_new = linspace(x(1),x(end),7);
u_new = interp1(x,u,x_new);
plot(x_new,u_new,'-o')
4 Comments
Voss
on 17 Jul 2023
u=[
0.0182
0.0253
0.0324
0.0404
0.0487
0.0552
0.0603
0.0643
0.0674
0.0699
0.0718
0.0732
0.0743
0.0751
0.0757
0.0762
0.0767
0.0770
0.0772
0.0773
0.0774
0.0775
0.0775
0.0775
0.0774
0.0772
0.0770
0.0767
0.0764
0.0760
0.0755
0.0749
0.0740
0.0730
0.0716
0.0699
0.0676
0.0646
0.0608
0.0559
0.0495
0.0416
0.0341
0.0273
0.0199
];
x = 1:numel(u);
plot(x,u)
hold on
nu = numel(u);
npts = 7;
idx = (1:npts:nu)+(0:npts-1).'
% in case nu is not a multiple of npts (e.g., 45 is not a multiple of 7),
% add some NaNs to the end of u, which will not affect the average except
% the last bin will have fewer non-NaN points than the other bins
if idx(end) > nu
bad_idx = idx > nu;
u_temp = [u; NaN(idx(end)-nu,1)];
else
bad_idx = [];
u_temp = u;
end
% average value of each group of npts values:
u_new = mean(u_temp(idx),1,'omitnan')
% average index of each group of npts values:
idx(bad_idx) = NaN;
x_new = mean(idx,1,'omitnan')
plot(x_new,u_new,'-o')
See Also
Categories
Find more on Histograms 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!