How do we Calculate Distance Matrix for Data Set in an Excel file
5 views (last 30 days)
Show older comments
Dear experiences...
i have a dataset D which includes N points in M dimensional space, data set stored in an excel file called (data.xls)
in this excel file ... (data view)
- first column (A) is my data points name (x1,x2,...xn),
- columns from (B ... M) are features where features are weighted according to some calculation.. where n in may data set = 127 and m=1200.
- Xij includes features weights for (i=1..n), (j=1..m)
*i need to calculate distance matrix based on (cosine distance)..where procedure i think its look like the following:
1- every row of Xi (data-point) is normalized to be (unite length=1) independent from others .. where the result matrix is includes normalized data points.
2- after that distance matrix applied based on cosine distance where cosine distance (i think) = 1-cosine similarity (dot product) .
i would thank any one can give me a help to import dataset in matlab and perform my requirements.. due i'm new to matlab?
0 Comments
Accepted Answer
Guillaume
on 13 Mar 2017
Edited: Guillaume
on 13 Mar 2017
1. Normalising the rows is easy:
NormalisedMatrix = OriginalMatrix ./ sqrt(sum(NormalisedMatrix .^ 2, 2));
2. Getting the cosine similarity is also fairly simple. I#m using the formula in this wikipedia article:
cossimilarity = @(a, b) sum(a.*b, 2) ./ sqrt(sum(a.^2, 2) .* sum(b.^2, 2));
similarity = squeeze(cossimilarity(OriginalMatrix, permute(OriginalMatrix, [3 2 1]))); %assumes R2016b
cosdistance = 1 - similarity;
The above gives you a NxN symmetric matrix of the similarity and distance between each vector.
8 Comments
Guillaume
on 14 Mar 2017
"can you please update this code to be work under 2015a"
I wrote, just above, "In previous versions:", followed by the two lines that need to be replaced to work in all versions before R2016b, including R2015a.
More Answers (0)
See Also
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!