How to reduce the length of a vector?

38 views (last 30 days)
Annabella la Grasta
Annabella la Grasta on 2 Sep 2020
Answered: Amrtanshu Raj on 11 Sep 2020
I want to reduce the length of the vector called "u1" and make it equal to 1x100.
Follows the code that I'm using:
H = smoothdata(z_a);
figure;
plot(tempo,H, 'g');
[pks,locs]=findpeaks(H, tempo, 'MinPeakProminence', 10,'Annotate', 'extents', 'WidthReference', 'halfheight');
figure;
findpeaks(H, tempo, 'MinPeakProminence', 10,'Annotate', 'extents', 'WidthReference', 'halfheight');
text(locs+.02,pks,num2str((1:numel(pks))'));
prev = 1;
prev1 = 1;
prev2 = 1;
for i = 1:length(pks)
indice1 = pks(i);
index1 = find(H==indice1);
u1 = H(prev:index1);
prev = index1;
accelerazione = z_b(prev1:index1);
prev1 = index1;
velocita = z_c(prev2:index1);
prev2 = index1;
q1 = (length(u1)/100)-0.01;
tempo1 = (0.0:0.01:q1);
tempo3 = (0.0:length(u1)-1);
time(i) = max(tempo1);
[miny1,minyidx1] = min(u1);
minx1 = tempo1(minyidx1);
figure;
subplot(2,1,1);
plot(tempo1,u1);
xlabel('Time(s)');
ylabel('Angolo');
avstr = sprintf('Ripezione n : %.1f ',i);
title(avstr);
hold on
xline(minx1,'r');
legend('Flesso estensione tronco','Finish');
%%media
media = mean(u1);
v(i) = media;
%%media accelerazione
media_acc = mean(accelerazione);
acc_media(i) = media_acc;
%%media velocita angolare
media_veloc=mean(velocita);
veloc_media(i) = media_veloc;
%%varianza
varianza = var(u1);
t(i) = varianza;
%%deviazione standard
deviazione = sqrt(varianza);
o(i) = deviazione;
subplot(2,1,2);
X_axis_normalize = (tempo3/(numel(tempo3)))*100;
plot(X_axis_normalize,u1);
[miny,minyidx] = min(u1);
minx = X_axis_normalize(minyidx);
hold on
xline(minx,'r');
legend('Flesso estensione tronco','Finish');
xlabel('%Cycle');
ylabel('Angoli');
figure;
subplot(2,1,1);
plot(tempo1,accelerazione);
hold on
plot(tempo1,ones(length(tempo1),1)*acc_media(i));
xlabel('Time(s)');
ylabel('Accelerazione(m/s^2)');
avstr1 = sprintf('Ripezione n : %.1f ',i);
title(avstr1);
gravstr1 = sprintf('Valore medio: %.1f ',acc_media(i));
legend('Accelerazione(m/s^2)', gravstr1);
subplot(2,1,2);
plot(tempo1, velocita);
hold on
plot(tempo1,ones(length(tempo1),1)*veloc_media(i));
xlabel('Time(s)');
ylabel('Velocità angolare(rad/s)');
gravstr2 = sprintf('Valore medio(rad/s): %.1f ',veloc_media(i));
legend('Velocità angolare(rad/s)', gravstr2);
end

Answers (1)

Amrtanshu Raj
Amrtanshu Raj on 11 Sep 2020
Hi,
I am assuming that you are trying to reduce the number of points in a signal while keeping the overall characteristics unchanged. You can interpolate 100 points in between the start and end values of the vector u1 along the vector u1. You can make use of the following code:
new_points = linspace(0,length(u1),100);
new_u1 = vals = interp1(u1,new_points);
You can explore the various option available for interpolation here.
Note - Reducing the data size by a huge value can lead to loss of some critical data points
And in case you just want to trim the u1 vector from start to end you can use the following:
u1(101:end) = []; % keep starting 100 values
u1(1:end-100) = []; % keep last 100 values

Community Treasure Hunt

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

Start Hunting!