Matlab counting the distance - matlab

I have a problem in computing the distance between two different matrices. The first matrix is 5000x6, the second matrix is 5x80.
I want to use this syntax to calculate the distances:
pdist2(mCe(1,:),row);
But this gives me an error saying "columns in x have to be same in y".
Is there a way to compute the distances when the matrices have a different amount of columns?

The pdist2 function calculates the distance between a set of points based on a metric. A metric is a function of 2 vector arguments from the same metric space and as such they are required to have the same dimension. What you want to do is not possible based on the definition of a metric. Read this link for more details
http://en.wikipedia.org/wiki/Metric_space

Related

Finding cosine Similarity between 2 csr_matrices

I have 2 csr_matrix. I want to compute cosine similarity between each vector of first matrix with each vector of 2nd matrix. For this purpose I am firstly building full vector including zeros from 1st matrix and then finding dot product product with every full vector of 2nd matrix. Is there any other way of doing this ? If the question is not clear , please let me know, however, I tried my best to explain.

MATLAB - Changepoint analysis or "findchangepts": How does it work?

I am using the function findchangepts and use 'linear' which detects changes in mean and slope. How does it note a change? Is it by consecutive points until the next point has a different mean and slope?
Mathworks has the following explanation:
If x is a vector with N elements, then findchangepts partitions x into two regions, x(1:ipt-1) and x(ipt:N), that minimize the sum of the residual (squared) error of each region from its local mean.
How does the function get ipt?
Thanks in advance!
I am working with a single vector with N elements.

Errors using distance metric in MATLAB

My test feature vector is 'testpg' and trained feature vector is 'trainpg' and both are of dimension 2000*1 .I am aiming to find the distance between the two histogram feature vectors and hence i do
distance = norm(trainpg-testpg)
Next I compare it to a scalar threshold value to check whether it satisfies my condition , The above code works well as i get a scalar value of this distance ie : for eg distance = 5.4 which is a scalar
But when I change code to use any other histogram based distance metric it doesn't work
i used the pdist2 function from http://www.mathworks.com/matlabcentral/fileexchange/29004-feature-points-in-image--keypoint-extraction/content/FPS_in_image/FPS%20in%20image/Help%20Functions/SearchingMatches/pdist2.m
The new code I used is
distance = pdist2(trainpg,testpg, 'chisq')
d = size(distance)
Here I am getting subscripted assignment dimension mismatch error as the dimensions of my distance are now 2000*2000 instead of 1*1
How could I get scalar value for the distances?
If the dimensions are 2000 x 1 for both of your feature vectors, then you should do:
distance = pdist2(trainpg.',testpg.', 'chisq');
pdist2 considers each row as a different sample. Thus you need to take the transpose, telling MATLAB that its a 2000-D feature of a single example rather than 1-D feature for 2000 examples. As a side note, you may want to check Histogram intersection distance, if your feature is a histogram. Look here for the code.

How can I weight the dimensions as I am computing KNN of given instance vector in MATLAB?

Suppose I have a bunch of instances and I want to find the closest K instances to a particular instance. Moreover, I have some weights showing the strengths of each dimension as we computing the distances. How can I incorporate these weights with the KNN finding process in MATLAB?
There are two methods that can allow you to do this. Looking at the knnsearch documentation, you can either use the seuclidean flag where this performs the standardized Euclidean distance. Each co-ordinate difference between two points is scaled by dividing by a corresponding scale value in S. S by default is the standard deviation for each co-ordinate. You can manually specify each of these scales by specifying the Scale parameter, then specifying a vector where each component will scale each dimension for you instead of the standard deviation in each dimension.
As such, the more contribution a co-ordinate has, the larger the scale should be, as you want to aggregate co-ordinates and will allow distances that are larger to have a smaller Euclidean distance. This is essentially the same thing as weighting the strengths in each dimension.
Alternatively, you can provide your own function that computes the distance between two vectors. You can define what these weights are in your workspace before hand, then create an anonymous function wrapper that accesses these weights when computing whatever distance measure you want yourself. The anonymous function can only take in two vectors, corresponding to two different co-ordinate vectors in KNN. As such, use this anonymous function to access the weights that should be already defined in the workspace then go from there.
Check out: http://www.mathworks.com/help/stats/knnsearch.html

how to measure the cosine similarity between 2 images

I have a population matrix of 5 images with 49 extracted salience features.
I want to calculate the cosine similarity in Matlab between a test image with the same extracted features 49.
1) Transform your images of size M lines X N columns in a vector M*N lines. Keep one image in a vector u and the other image in a vector v.
2) Evaluate: cosTheta = dot(u,v)/(norm(u)*norm(v)); [As far as I know there is no function in matlab that does that]
Usually people evaluate similarities among images using the projections of them on the eigenfaces. So, before doing that, people usually evaluate the eigenfaces.
You could use the matlab's built in function to get the cosine distance:
pdist([u;v],'cosine')
which returns the "One minus the cosine of the included angle between points". You could then subtract the answer from one to get the 'cosine of the included angle' (similarity), like this:
1 - pdist([u;v],'cosine')
Source: Pairwise distance between pairs of objects.