Create a join on two cell-Arrays
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I've got two cell-arrays:
A = {'id1','H20';
'idc','O2';
'id3','CO2'};
B = {'idc';'id1';'id3';'id1';'id1'};
After running my script, I want B modified that it contains:
B = {'idc','O2';
'id1','H2O';
'id3','CO2';
'id1','H2O';
'id1','H2O'};
Has anyone an idea avoiding loops? Thanks :)
0 件のコメント
採用された回答
その他の回答 (3 件)
Jan
2011 年 9 月 22 日
ISMEMBER, INTERSECT and SETDIFF are very powerful and optimized to work on huge data sets. But if you operate on cell strings with < 1000 elements, some simple loops are usually much faster:
A = {'id1','H20';
'idc','O2';
'id3','CO2'};
B = {'idc';'id1';'id3';'id1';'id1'};
function B = myFunc(A, B)
index = zeros(numel(B), 1);
for i = 1:size(A, 1)
index(strcmp(B, A{i})) = i; % A{i} == A{i,1}
end
B = A(index, :);
This is 27 times faster than the ISMEMBER approach for your tiny dataset. Therefore I would not call such FOR loops "dirty workaround".
[EDITED] Using the index method instead of the former method to create the column cell directly is 40% faster.
0 件のコメント
Vincent
2011 年 9 月 22 日
1 件のコメント
Jan
2011 年 9 月 22 日
This function vanishes inside a subfunction such that the actual program contains "B=myFunc(A,B)" only, which is easy to read.
I'd be very interested in a speed comparison of the two methods for the larger dataset. I modify my example to work for cells with 40 columns in a few minutes.
I've converted an equivalent function into a C-Mex: http://www.mathworks.com/matlabcentral/fileexchange/24380-cstrainbp
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!