How do I modify my x data to be a specific length?

I have some x data and y data which I want to plot.
My x data looks like this: [326,406,406,449,491,491,1353,1353,2379,2379, ...]
the corrosponding y data looks like this: [3.46,3.48,3.48,3.48,3.49,3.49,3.49,3.49,3.49,3.49, ...]
Imagine I want to modify my x data in such a way that it is of fixed length.
But simply slicing like this for exmaple: x = x(1:100) would not be good, because then I would also loose the data after the 100th x data point.
I need it to be this way: Having x data as an array of fixed length but also modifying y data in such a way that I don't see a difference when I plot y vs x (except for the fact that x now has a different length and different values, e.g. from 1:100).
I hope you understand what I'm trying to do. I was playing around with interp1 but sadly I did not achieve anything useful.
Thanks in advance!

1 Comment

For better illustration:
Imagine my y vs. x data looks like this:
My goal is to simple modify my x and y data in such a way that the x data would range from 1:100 for example, but the curve would still be looking the same.
So basically: Sampling the data

Sign in to comment.

 Accepted Answer

I am not certain what you want.
Try this —
x = [326,406,406,449,491,491,1353,1353,2379,2379];
y = [3.46,3.48,3.48,3.48,3.49,3.49,3.49,3.49,3.49,3.49];
[xu,ix] = unique(x);
yu = y(ix);
N = 100; % Length Of Interpolation Vector
xq = linspace(min(xu), max(xu), N);
yq = interp1(xu,yu,xq);
figure
subplot(2,1,1)
plot(x, y, '.-')
grid
subplot(2,1,2)
plot(xq, yq, '.-')
grid
The original data appear to be the result from a stairs plot, so consider using that function for the second plot, if necessary.
.

4 Comments

Use it if it works for you. There is likely no one ‘best’ function to use unless you have specific requirements, such as signal processing, that need to incorporate an anti-aliasing filter.
I generally prefer the resample function, because I usually use it for signal processing applications and so am used to it and understand it. The interp1 function is what I usually use in situations not involving signal processing, largely because it also has a number of method options, and again, because I use it frequently and understand it.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!