How to match two matrices in time while having different sizes?

I am having two matrices each with one column and both having the same starting and ending points (for example, t=0 and t= 500). However, each one of them have different increments and hence the sizes differ. I want to match the two matrices with respect to these starting and ending points even if I loss some data points in between. I have tried the "ismember" command but this one is used to find the similarities for each value in the matrix while I want to match the positions rather than the values of the matrix. Any suggestions?

Answers (2)

What does that mean exactly? Can you give a small example? For now, all I can suggest is to find out which vector is longer and then use splines to "fill in" the "missing" locations on the shorter vector. This will create values in between the common values while making sure your "knot" values (values in common) are in the same location in both vectors. I attach a spline example, though since it's an existing demo it needs some tweaking to do what you're asking.

14 Comments

I don't think this is what I need. For example, I produced two one column data sets one with values from 1 to 37 with an increment of 1 and the other one is from 1 to 37 but with an increment of 1.5. I need to match those two data sets so the longer one will lose some of its values in order to become the same size as the second matrix but without ruining the data of the longer matrix(it will still give similar graph if it was plotted). How could this be done?
Thank you for your help
OK, this is my third request (it might be my last): "Can you give a small example?"
Include both input arrays and what you'd like to see in your output.
x=[1:37]';y=[1:1.5:37]';% let say these are force and displacement data and they have the same time but different sizes because of the increment
A=resample(x,2,3);
B=[A y];%I want to obtain this B but without damaging the accuracy and usefulness of the data
This is a sample code.
I don't understand. You gave code that actually does run. Is that B not what you want? How does your code damage "the accuracy and usefulness of the data"??? If that B is no good, then what would you like for B?
I don't know if it damages the data, when I plotted the data I got a drop in B that did not exist before the resampling so my question is how to do the same thing but without getting this difference if it is possible. I am sorry for the ambiguity in my question.
What was B before you did B=[A y]? Let me see the code that you used to plot the "before" B, and the "after" B.
Read this and this and then attach a screenshot
The B did not exist before the resampling because the two matrices have different sizes. The first figure shows the plot for both x and y (red and blue respectively). The second figure shows the resampled x matrix which shows a drop that did not exist before. Is it possible to reach the same B which contains two matrices with the same sizes without getting this drop? Again, thank you so much for your help and its appreciated and sorry for the inconvenience.
I'm getting lost. I see no reason why there should be little wiggles in the plot. Please post your entire script so we can examine it.
Attached is the code with the two excel files I have been using for testing. If you plot the matrix y you will get the little disturbance I just showed in figure 2.
That code does not make any plots. Please post the code you used. Also, I think you want to plot load vs. strain or vice versa. Yet all your commented out plot lines do is plot versus the index along the x axis. Why would you want to do that? What do you want to be along the x axis (load or strain), and what do you want to be along the y axis (load or strain)?
The first figure in the attached code shows the comparison between the original data and the obtained resampled x. The second figure shows what I want to obtain which is load vs strain and my problem is the disturbance in it because if the data sets were much larger I cannot have this much disturbance in it.
I don't understand why you don't have a load for every strain value. Why don't they match up? Since they don't, what load values were being applied when the stain values were applied? You can't just say that you have 37 load values and 25 strain values without knowing what strain and load pairs match up. I mean, there are 12 extra load values, so WHERE did they come from? What strain was in effect when those load values were measured?
I don't have the data yet so I was hoping there is a way just to sample the longer signal to fit it in size but I guess I will have to wait for the data and then proceed. Thank you for your help and time. It is most appreciated.

Sign in to comment.

Not clear what you intend, precisely, no...but--
If you're meaning to pick points from the more densely populated that correspond to the same relative position in the shorter, then if you have Signal Processing Toolbox given
x=shorterSignal;
y=longerSignal;
ynew=resample(y,length(x),length(y),0); % resample y to same length as x
W/O the Toolbox it's about as simple--
ynew=interp1(1:length(y),y,1:length(x),'nearest'); % pick nearest integer index

3 Comments

As an example:
shorterSignal = [1, 4, 100, 500]
longerSignal = [2, 4, 100, 200, 317, 431, 457]
x=shorterSignal;
y=longerSignal;
ynewTB = resample(y,length(x),length(y),0) % resample y to same length as x
ynewNoTB =interp1(1:length(y),y,1:length(x),'nearest') % pick nearest integer index
Produces:
shorterSignal =
1 4 100 500
longerSignal =
2 4 100 200 317 431 457
ynewTB =
2 100 317 431
ynewNoTB =
2 4 100 200
Is this what is wanted? Hence the need to clarify the request with sample input and output data.
I was actually using the "resample" command but the problem is I am getting a drop at the end of the data set. What should I do to fix that? Thank you for your help
Don't know...what do you mean, precisely? Show an example...

Sign in to comment.

Asked:

M
M
on 26 Jun 2016

Commented:

dpb
on 26 Jun 2016

Community Treasure Hunt

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

Start Hunting!