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 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