Find matches without repetition from two cell arrays + extra condition

1 回表示 (過去 30 日間)
Alberto Cuadra Lara
Alberto Cuadra Lara 2018 年 12 月 6 日
Hello,
In order to reduce the size of a matrix I need to find all the elements of the periodic table (patterns - cell 1) of all the species considered (cell 2). Also, it has to take in account every number as the end of the evaluable pattern.
Example:
Elements = {'H', 'He', 'Li', .... };
NameSpecies = {'H2','O2','H2O2','CH2OH','C'};
Desired_Result = {'H','O','C'};
At the moment I'm using this piece of code that I found on the net
Match=cellfun(@(x) ismember(x,NameSpecies), Elements, 'UniformOutput', 0);
Elements = Elements(cell2mat(Match));
but this function is only going to give me the same individuals elements, in the case of the example the result is
Result = {'C'};
Any suggestion to tackle it without brute force?
Thanks for your time!
  2 件のコメント
madhan ravi
madhan ravi 2018 年 12 月 6 日
what's the pattern ?
Desired_Result = {'H','O','C'};
Alberto Cuadra Lara
Alberto Cuadra Lara 2018 年 12 月 6 日
Bruno has already resolved the question. Okay pattern maybe was not the best description of the problem. The problem consisted to find the common elements of the set of species considered.
Thanks.

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

採用された回答

Bruno Luong
Bruno Luong 2018 年 12 月 6 日
You don't even need the period table, assuming element start with an Uppercase eventually followed by lower-case
NameSpecies = {'He','O2','H2O2','CH2OH','C'};
Tmp = cell(size(NameSpecies));
for k = 1:length(Tmp)
Specie = NameSpecies{k};
Specie(Specie>='0' & Specie<='9') = ' ';
idx = find([(Specie>='A' & Specie<='Z'), true]);
lgt = diff(idx);
Tmp{k} = strtrim(mat2cell(Specie, 1, lgt));
end
El = unique(cat(2,Tmp{:}))
The result is:
El =
1×4 cell array
{'C'} {'H'} {'He'} {'O'}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by