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!