'closeness' of multiple vectors

조회 수: 3 (최근 30일)
Philip
Philip 2012년 4월 22일
Is there a way to compute how 'closely' a data vector matches another? For example:
A = [1 2 3 4 5];
B = [1 2 3 4 5];
C = [6 7 8 9 10];
Can I use vector A to compare the closeness against vector B, and then vector C? For (A,B), the closeness should be 100%, where as for (A,C) it should be 0%...

채택된 답변

Richard Brown
Richard Brown 2012년 4월 22일
Is closeness a measure of how many entries match in matching positions? In this case:
closeness = @(x, y) nnz(x == y) / numel(x);
A = [1 2 3 4 5];
B = [1 2 3 4 5];
C = [6 7 8 9 10];
closeness(A, B)
closeness(A ,C)
If your vectors can have noninteger entries, then to mitigate against floating point errors you'd replace x == y with abs(x - y) < tol * abs(x) where tol is something small, like 1e-14.
  댓글 수: 3
Richard Brown
Richard Brown 2012년 4월 23일
haha, yes that's better :)
Philip
Philip 2012년 4월 23일
That's perfect! Thanks to you both for your quick answers!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by