How to get the row index of most similar row

2 ビュー (過去 30 日間)
Mekala balaji
Mekala balaji 2018 年 8 月 31 日
コメント済み: Stephen23 2018 年 9 月 1 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by