How to create comparison matrices "comparing" strings from 6 different data sources?

3 ビュー (過去 30 日間)
I have data from 4 different sources in a form of strings. I want to compare which strings are the same and if they are not equal count how many times they disagree.
So if the first set of strings is:
array{1}={'111','113','111','111'}
strings 1,3 and 4 are the same, and string 2 is different to all, so I will like to receive a matrix where similar strings have the same number, and one matrix that counts the number of differences when compared with the first string (the first column should be always zero), like this:
c(1,:)=[1 2 1 1]
d(1,:)=[0 1 0 0] % normaly I use strcmp
if all the strings were different I will like to receive something like this:
c(n,:)=[1 2 3 4]
or all the same
c(n,:)=[1 1 1 1]
d(n,:)=[0 0 0 0]
if a string is missing get an NaN
c(n,:)=[1 NaN 1 1]
d(n,:)=[0 NaN 0 0]
so for this arrays:
array{1}={'111','113','111','111'}
array{2}={'111','', '221','221'}
array{3}={'111','113','' ,'113'}
array{4}={'' ,'133','133','221'}
array{5}={'231','231','111','231'}
I would like to recieve smoething like:
c = [1 2 1 1;
1 NaN 3 3;
1 2 NaN 2;
NaN 2 2 4;
1 1 3 1]
Each row represent a comparison of a file between different sources, columns represent the sources, each of the elements of the matrix can take values form 1-4 or NaN.
Column 1 have values of 1 or NaN
Column 2 have values of 1,2 or NaN
Column 3 have values of 1,2,3 or NaN
Column 4 have values of 1,2,3,4 or NaN
And the differences:
d = [0 1 0 0;
0 NaN 2 2;
0 1 NaN 1;
NaN NaN NaN NaN; % since the first value is missing
0 0 2 0]
I did that using a lot of IF and the script looks so ugly haha, "If" you can recomend me something I would really appreciate it

採用された回答

Stephen23
Stephen23 2016 年 7 月 15 日
編集済み: Stephen23 2016 年 7 月 15 日
Your data:
array{1}={'111','113','111','111'};
array{2}={'111','', '221','221'};
array{3}={'111','113','' ,'113'};
array{4}={'' ,'133','133','221'};
array{5}={'231','231','111','231'};
The calculation, based on unique:
idz = cellfun('isempty',vertcat(array{:}));
% Comparison:
fun = @(x)unique(x,'first');
[~,idx,idy] = cellfun(fun,array,'UniformOutput',false);
C = cellfun(@(x,y)x(y),idx,idy,'UniformOutput',false);
C = vertcat(C{:});
C(idz) = NaN
% Difference:
D = C-1;
D(bsxfun(@or,idz,idz(:,1))) = NaN
and the outputs:
C =
1 2 1 1
1 NaN 3 3
1 2 NaN 2
NaN 2 2 4
1 1 3 1
D =
0 1 0 0
0 NaN 2 2
0 1 NaN 1
NaN NaN NaN NaN
0 0 2 0
  1 件のコメント
Guillaume
Guillaume 2016 年 7 月 15 日
In my version of matlab (r2016a), 'first' is the default option of unique so could be omitted.
Also, in my version of matlab, the 2nd and 3rd return of matlab are documented as returning column vectors. So I assume the above, use the 'legacy' mode of unique, so fun should be modified to:
fun = @(x) unique(x, 'legacy');
Alternatively, modify the C generation to:
C = cellfun(@(x, y) reshape(x(y), 1, [], idx, idx, 'UniformOutput', false);
With the reshape, it does not matter if unique returns rows or columns.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by