down sampling a vector keeping the order of elements values

4 次查看(过去 30 天)
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

回答(1 个)

Voss
Voss 2023-7-17
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 个评论
Ham Man
Ham Man 2023-7-17
something like this, but keeping all data in, without ignoring some part of data (here: the first 3 and last data are excluded).
averaging the first 7 data, then 8th to 14th, and so on so forth, then finally get a vector of seven points.
Voss
Voss 2023-7-17
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).'
idx = 7×7
1 8 15 22 29 36 43 2 9 16 23 30 37 44 3 10 17 24 31 38 45 4 11 18 25 32 39 46 5 12 19 26 33 40 47 6 13 20 27 34 41 48 7 14 21 28 35 42 49
% 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')
u_new = 1×7
0.0401 0.0709 0.0768 0.0773 0.0745 0.0586 0.0271
% average index of each group of npts values:
idx(bad_idx) = NaN;
x_new = mean(idx,1,'omitnan')
x_new = 1×7
4 11 18 25 32 39 44
plot(x_new,u_new,'-o')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by