How can we find the non common strings from multiple columns

8 ビュー (過去 30 日間)
ektor
ektor 2017 年 3 月 10 日
コメント済み: ektor 2017 年 3 月 13 日

Dear all,

I want to compare the following three columns of strings (of different length) and find the entries that are NOT common across them. Just to mention that my real vectors (listA,listB,listC, etc) are of large size.

    listA={'India'
    'Iran'
    'Ireland'
    'Israel'
    'Jordan'
    'Kuwait'
    'Lebanon'
    'Lesotho'
    'Libya'
    'Luxembourg'
    'Malawi'
    'Maldives'
    'Morocco'
    'Nepal'
    'Netherlands'
    'Norway'
    'Oman'
    'Paraguay'
    'Peru'
    'Philippines'
    };
 listB={    'Denmark'
    'El Salvador'
    'Gabon'
    'Gambia'
    'Lebanon'
    'Lesotho'
    'Libya'
    'Luxembourg'
    'Malawi'
    'Maldives'
    'Morocco'
    'Nepal'
    'Netherlands'
    'Norway'
    'Oman'
    'Paraguay'
    'Peru'
    'Philippines'
    'Qatar'
    'Rwanda'
    'Saudi Arabia'
    'Senegal'
    'Singapore'
    'Sudan'};
 listC= {'Kuwait'
    'Lebanon'
    'Lesotho'
    'Nepal'
    'Netherlands'
    'Oman'
    'Sweden'
    'Syria'
    'Tanzania'
    'Turkey'
    'Uruguay'};
  1 件のコメント
ektor
ektor 2017 年 3 月 13 日
any suggestions? Thanks in advance.

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

採用された回答

Stephen23
Stephen23 2017 年 3 月 13 日
編集済み: Stephen23 2017 年 3 月 13 日
First lets find the strings that are common to all cell arrays:
listA={'India','Iran','Ireland','Israel','Jordan','Kuwait','Lebanon','Lesotho','Libya','Luxembourg','Malawi','Maldives','Morocco','Nepal','Netherlands','Norway','Oman','Paraguay','Peru','Philippines',};
listB={'Denmark','El Salvador','Gabon','Gambia','Lebanon','Lesotho','Libya','Luxembourg','Malawi','Maldives','Morocco','Nepal','Netherlands','Norway','Oman','Paraguay','Peru','Philippines','Qatar','Rwanda','Saudi Arabia','Senegal','Singapore','Sudan'};
listC={'Kuwait','Lebanon','Lesotho','Nepal','Netherlands','Oman','Sweden','Syria','Tanzania','Turkey','Uruguay'};
%
lists = {listA,listB,listC}; % storing data together makes working with them much simpler.
%
exc = lists{1};
for k = 2:numel(lists)
exc = intersect(exc,lists{k});
end
giving the strings found in all cell arrays as:
exc =
'Lebanon'
'Lesotho'
'Nepal'
'Netherlands'
'Oman'
Now we can easily get the input cell arrays with the strings from exc removed:
out = cell(size(lists));
for k = 1:numel(lists)
out{k} = setdiff(lists{k},exc);
end
giving:
>> out{:}
ans =
'India' 'Iran' 'Ireland' 'Israel' 'Jordan' 'Kuwait' 'Libya' 'Luxembourg' 'Malawi' 'Maldives' 'Morocco' 'Norway' 'Paraguay' 'Peru' 'Philippines'
ans =
'Denmark' 'El Salvador' 'Gabon' 'Gambia' 'Libya' 'Luxembourg' 'Malawi' 'Maldives' 'Morocco' 'Norway' 'Paraguay' 'Peru' 'Philippines' 'Qatar' 'Rwanda' 'Saudi Arabia' 'Senegal' 'Singapore' 'Sudan'
ans =
'Kuwait' 'Sweden' 'Syria' 'Tanzania' 'Turkey' 'Uruguay'
And if you want a list of all strings together, excluding those in exc:
uni = {};
for k = 1:numel(lists)
uni = union(uni,lists{k});
end
uni = setdiff(uni,exc)
gives:
uni =
'Denmark' 'El Salvador' 'Gabon' 'Gambia' 'India' 'Iran' 'Ireland' 'Israel' 'Jordan' 'Kuwait' 'Libya' 'Luxembourg' 'Malawi' 'Maldives' 'Morocco' 'Norway' 'Paraguay' 'Peru' 'Philippines' 'Qatar' 'Rwanda' 'Saudi Arabia' 'Senegal' 'Singapore' 'Sudan' 'Sweden' 'Syria' 'Tanzania' 'Turkey' 'Uruguay'

その他の回答 (1 件)

Jan
Jan 2017 年 3 月 13 日
編集済み: Jan 2017 年 3 月 13 日
The question is not completely clear. Do you want to find the strings, which appear only in one of the lists? Or do you want to exclude only the strings, which occur in all 3 lists?
For the first case:
R = setdiff(listA, listB);
R = setdiff(R, listC);
  1 件のコメント
ektor
ektor 2017 年 3 月 13 日
Hi Jan. Thank you very much for your reply. I want to exclude only the strings which occur in all 3 lists.
If you can also advice me how to handle the case of more than 3 lists, e.g 8 lists that would be great! Many thanks again

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

カテゴリ

Help Center および File ExchangeModulation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by