How do we Calculate Distance Matrix for Data Set in an Excel file
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
Guillaume
2017년 3월 13일
편집: Guillaume
2017년 3월 13일
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
Guillaume
2017년 3월 14일
"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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!