Finding elements of matrix that match a condition

Hello everyone,
I am working with some big data sets and I wanted to find some elements from one matrix that satisfy a condition. So, what I have. Matrix A is 251576x5. Column 1 is a timestamp, columns 2 to 4 are x,y,z coordinates and column 5 is something else that is not important to me. Matrix B is 26300x1 and is a column of timestamps starting from 0 and incrementing by 100. What I want is, for each timestamp on B I want the row with that same, or the closest possible, timestamp from A saved in another Matrix. So my output should be a matrix C, 26300x5 in dimensions.
For example, this is a piece of matrix A
25194.41 313.93 440.32 372.93 274
25727.49 327.04 420.64 387.72 277
A timestamp from matrix B is
25200
So what would be saved here in matrix C should be 25194.41 313.93 440.32 372.93 274.
I hope I made myself clear enough. Thank you all in advance

Réponses (1)

You can use interp1() with nearest option
Bx = interp1(A(:,1), A(:,2:4), B, 'nearest');
C = [B Bx];

9 commentaires

Thank you for your swift reply. I am getting this error message
Error using griddedInterpolant
The grid vectors must contain unique points.
Error in interp1 (line 148)
F = griddedInterpolant(Xext,V,method);
Error in my_script (line 31)
Bx = interp1(A(:,1), A(:,2:4), B, 'nearest');
Ameer Hamza
Ameer Hamza le 14 Oct 2020
Modifié(e) : Ameer Hamza le 14 Oct 2020
Are there duplicate timestamps in first column of matrix A? Can you share your matrices in a .mat file?
Ioannis Kiratzis
Ioannis Kiratzis le 14 Oct 2020
Modifié(e) : Ioannis Kiratzis le 14 Oct 2020
It appears to work only if A and B have the same number of rows
No, it works when they have different number of rows too
A = [linspace(0, 100).' rand(100, 3)];
B = linspace(0, 100, 1000).';
Bx = interp1(A(:,1), A(:,2:4), B, 'nearest');
C = [B Bx];
Ioannis Kiratzis
Ioannis Kiratzis le 14 Oct 2020
Modifié(e) : Ioannis Kiratzis le 14 Oct 2020
I had to chop matrix A down a bit but it shouldn't make a difference. Choped due to limitations of upload file size
It works even for different sizes you are right. It's weird. it works when I chop A down to 100000 lines but if I go above it doesn't work anymore
The code works correct on the matrices you shared? Can you share matrices where it fails? Also check of there are any duplicate time stamps in matrix A.
I can't share thethe complete matrices were it fails as they exceed the limit on the file size I can upload. I can work around the problem. I could work on a smaller portion of the dataset anyway since these are results from long experiments. So you are saying the problem is the presence of duplicate timestamps in A?
Yes. If that happens, then the interpolation will fail. I guess that is the most likely reason for the error you are receiving.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by