How to find the nearest matrix between two matrices?

6 Ansichten (letzte 30 Tage)
MP
MP am 18 Jul. 2022
Kommentiert: MP am 19 Jul. 2022
Find the nearest row matching between matrix.
I have a matrix
A = [2013 03 17 1 6 0]; % 1x6 double
I have another matrix
B = [2013 3 17 0 5 1.012; 2013 3 17 1 6 20.47]; % 4x6 double
I want to find the index of the nearest matrix A in B.
Can anyone please help?
Any help will be greatly appriciated.

Akzeptierte Antwort

Rik
Rik am 18 Jul. 2022
Convert each row to a date format (either datetime or datenum).
After that you can subtract one from the other, use abs and min to find the smallest difference. Use the second output of min to find the index of the smallest difference.
  1 Kommentar
Stephen23
Stephen23 am 18 Jul. 2022
Bearbeitet: Stephen23 am 18 Jul. 2022
A = [2013,3,17,1,6,0]; % 1x6 double
B = [2013,3,17,0,5,1.012; 2013,3,17,1,6,20.47]; % 4x6 double
The best way of storing timestamps is as DATETIME objects:
Adt = datetime(A)
Adt = datetime
17-Mar-2013 01:06:00
Bdt = datetime(B)
Bdt = 2×1 datetime array
17-Mar-2013 00:05:01 17-Mar-2013 01:06:20
Then just use ABS and MIN exactly as Rik recommended:
[~,idx] = min(abs(Adt-Bdt))
idx = 2
Bdt(idx)
ans = datetime
17-Mar-2013 01:06:20

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Abderrahim. B
Abderrahim. B am 18 Jul. 2022
Bearbeitet: Abderrahim. B am 18 Jul. 2022
clear
A = [2013,3,17,1,6,0]; % 1x6 double
B = [2013,3,17,0,5,1.012; 2013,3,17,1,6,20.47]; % 4x6 double
A = datetime(A) ;
B = datetime(B) ;
minSize_B = max(size(B)) ;
nearestAB = [] ;
for ii = 1:minSize_B
nearestAB = [nearestAB abs(A - B(ii))] ;
end
[~, nearestB_indx] = min(nearestAB) % Means, the 4th row in B
nearestB_indx = 2
nearestB = B(nearestB_indx)
nearestB = datetime
17-Mar-2013 01:06:20
  5 Kommentare
Rik
Rik am 18 Jul. 2022
@MP did you see my answer? Stephen already implemented it for you in a comment.
MP
MP am 19 Jul. 2022
Yes, @Rik, thanks for helping. I was trying how to implement it but @Stephen23's comment did a great help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by