How to calculate if two matrices have similar elements.

I've got a number of different csv files which have the following format;
Number, Number, Number, Number, Number, Text.
Below are some examples.
3764.92,3764.92,-759.167,58.8299,65.4035,1,One
165.493,3234.51,-1940.46,311.928,63.8692,1,Three
I was wondering if its at all possible to calculate if 2 different matrices have similar elements (the same rows) and how many of the same elements they have. There are 34 columns per file.
Any help would be much appreciated!

 採用された回答

Dishant Arora
Dishant Arora 2012 年 8 月 27 日
編集済み: Dishant Arora 2012 年 8 月 27 日

0 投票

total=0;
for num=1:34
if isequal(file1(num),file2(num))
total=total+1;
end
end
sum will return the number of same elements

5 件のコメント

Walter Roberson
Walter Roberson 2012 年 8 月 27 日
Please do not use "sum" as a variable name, as that conflicts with the important MATLAB routine "sum". This has caused problems for a number of people.
Dishant Arora
Dishant Arora 2012 年 8 月 27 日
ok, thanks.
Thomas
Thomas 2012 年 8 月 27 日
Thanks very much for the assistance Dishant, however I'm not sure this is doing what I want it to do. I have other files (which are 593 rows in length) which should have many different rows in common, and on manual inspection they do, however this system isn't picking them up.
I think its because in some cases line 2 in file 1 will be the same as line 20 of file 2, and its not picking this up, my apologies if I phrased my question poorly initially.
Walter Roberson
Walter Roberson 2012 年 8 月 27 日
Your values are floating point. Floating point values that are not calculated exactly the same way often have round-off differences, and so will not compare equal.
Dishant Arora
Dishant Arora 2012 年 8 月 27 日
編集済み: Dishant Arora 2012 年 8 月 27 日
bascially you want number of common elements in the files?? does the rows here follow the same pattern as above , i mean with last element as string and all other elements of class double(numbers).
if this is the case, try this:
mat1=cell2mat(file1(1:end-1));
mat2=cell2mat(file2(1:end-1));
total=length(intersect(mat1,mat2))+isequal(file1(end),file2(end));

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by