MATLAB Answers


Compare two vectors for similarity

How to compare two vectors quickly. Right now I print out each in a loop and examine them by eye, is there a way i can find if two are almost similar.

  1 Comment

I am comparing A to B and then A to C, I need a single number that will allow me to quickly judge A resembles B or C.



No products are associated with this question.

2 Answers

Answer by Matt Fig
on 9 Dec 2012
 Accepted answer

What is the criteria for 'almost similar' in your application? 90% same exact values? 90% of the values in one vector within 95% of some other value in the other vector? Do the values have to be in the same positions? Do the vectors have to be the same length? Perhaps a few short examples would help...


I am using lpc(x,4), x is a set of discrete data to get 4 coeff vector of a linear prediction filter. Then I am comparing these coeff to another set that i get from executing lpc over another part of the same x(n) to find which two parts resemble the closest.

@Souparno: Accepting an answer means that the problem is solved. Is this true here?

yes, S = sum(A-B), is what I was looking for.

Answer by Greg Heath
on 10 Dec 2012

S = sum(A-B) is NOT a useful function for quantifying similarity because positive and negative terms will cancel.

The most common are

mae(A-B) % mean(abs(A-B))

sae(A-B) % sum(abs(A-B))

norm(A-B,1) % sum(abs(A-B))

norm(A-B,inf) % max(abs(A-B))

mse(A-B) % mean((A-B).^2)

sse(A-B) % sum((A-B).^2)

norm(A-B) % sqrt(sse(A-B))

Hope this helps.

Thank you for formally accepting my answer



Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!