How to get the row index of most similar row

2 次查看(过去 30 天)
hi, I have below array(double array):
database:
1.0 2.02 3.2 3.56
0.0 3.30 1.2 9.3
1.2 3.80 1.23 9.3
2.0 3.30 1.2 7.3
1.11 3.63 1.04 9.43
current:
1.23 3.7 1.01 9.3
I want to get the latest most closely similar row,
In the this example row 3 & row 5 are similar based on summation of delta between current to each row in database. But, the row 3 is more similar with less variation considering each element. My desired output is row3.

采纳的回答

Stephen23
Stephen23 2018-8-31
编辑:Stephen23 2018-8-31
The fifth row seems to be closest (in a least-squares sense):
>> A = [1.0,2.02,3.2,3.56;0.0,3.30,1.2,0.3;1.2,3.80,1.23,9.3;2.0,3.30,1.2,7.3;1.11,3.63,1.04,9.43];
>> B = [1.23,3.7,1.01,9.3];
>> [~,idx] = min(sum((A-B).^2,2))
idx = 5
The fifth row also has lower variance than the third row:
>> var(B-A(3,:))
ans = 0.012758
>> var(B-A(5,:))
ans = 0.012292
If you really need to match the third row, then please provide an actual mathematical function (not words, not a description, not a wish, etc.) that returns a minimum (or maximum) metric corresponding to that row.
  2 个评论
Mekala balaji
Mekala balaji 2018-9-1
编辑:Stephen23 2018-9-1
Sir, That's fine, but if there are multiple rows with same sum of least square error and want to catch the last one of it. I use the below command, but it caught below error:
Error using min
MIN with two matrices to compare and two output arguments is not supported.
[~,idx] = min((sum((A-B).^2,2)),'last')
I modified the input to test this:
A = [1.0,2.02,3.2,3.56;0.0,3.30,1.2,0.3;1.2,3.80,1.23,9.3;1.2,3.80,1.23,9.3;2.0,3.30,1.2,7.3;1.89,3.79,2.04,9.43];
when use:
[~,idx] = min((sum((A-B).^2,2)))
idx =
3
but the idx should be 4(which is the last one)
Stephen23
Stephen23 2018-9-1
@Mekala balaji: I looked carefully at the min documentation, but could not find any mention of a 'last' input option. Where did you see this?
Both min and max return the indices of the first occurrence, exactly as described in the documentation. So if you want to get the last occurrence, then just flip its input array along the required dimension:
>> A = [1.0,2.02,3.2,3.56;0.0,3.30,1.2,0.3;1.2,3.80,1.23,9.3;1.2,3.80,1.23,9.3;2.0,3.30,1.2,7.3;1.8
9,3.79,2.04,9.43];
>> B = [1.23,3.7,1.01,9.3];
>> [~,idx] = min(flipud(sum((A-B).^2,2)));
>> idx = 1+size(A,1)-idx
idx = 4

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by