how to bring 5 vectors with differing length in line and plot it
3 次查看(过去 30 天)
显示 更早的评论
Hello community,
i want to bring 5 vectors wich all have different lengths in line together, so that each has the same length.
I have measured a force 5 times. each measurement was about to measure the force of a sliding part in a special movement. i pushed "start measurement" and when the movement was finished i hit "stop measurement". This caused that each measurement of the force took different time, wether i stopped or startet it later / earlier. Now i have 5 force vectors... one is 28000x1 the other is 31098x1 the third is 27001 and so on... my target is to plot all graphs over the movement. and therefore i need them to be the same length... wether i do it with interpolation, something like linspace or anything else, i dont know.
Anyone an idea?
Thanks,
0 个评论
回答(2 个)
Andrew
2012-11-21
Hello Florian,
if you want to bring all in one line, you can do it in this way:
a1=[1,2,3,4]; % measurement 1
a2=[5,6,7]; % measurement 2
a3=[8,9]; % measurement 3
A=[a1,a2,a3] % all in one line
Jan
2012-11-21
编辑:Jan
2012-11-21
Why do the vectors need the same length?
x1 = 1:10;
y1 = rand(1, 10);
x2 = 1:12;
y2 = rand(1, 12) + 0.5;
plot(x1, y1, 'r');
hold on;
plot(x2, y2, 'b');
So you can draw them together with different sizes.
But if you really need an interpolation:
- Get the size of the longest data set.
- Interpolate the others to this length using:
yNew = interp1(xi, yi, linspace(xi(1), xi(end), lengthOfLongest));
where "xi" and "yi" are the data of the other measurements.
[maxLen, maxIndex] = max(cellfun('prodofsize', x));
y2 = cell(1, 5);
for ii = 1:5
if ii == maxIndex
y2{ii} = y{ii};
else
xi = x{ii};
y2{ii} = interp1(xi, y{ii}, linspace(xi(1), xi(end), maxLen));
end
end
Now all y2 have the same length.
2 个评论
Jan
2012-11-21
It would be easier if you explain, how you store the 5 data sets. Then I would not have to guess this detail. Posting some code helps to clear such details.
When I assume, that you store the values in the variables "x1,x2,x3,x4,x5" and "y1,y2,y3,y4,y5" (what is actually a bad idea, because it is so hard to expand this list to 25), and "x2" is the longest data set, then "xi(end)" means "x1(end)", "x3(end), "x4(end)", "x5(end)". Then LINSPACE creates as many data points as the longest data set from "xi(1)" to "xi(end)".
I post a method with the much smarter choice of using a cell. See [EDITED]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multirate Signal Processing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!