How to get the row index of most similar row
1 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!