Create a join on two cell-Arrays

조회 수: 1 (최근 30일)
Vincent
Vincent 2011년 9월 22일
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 :)

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 22일
[tf, aidx] = ismember(B, A(:,1));
B = [B, A(aidx,2)];
  댓글 수: 1
Jan
Jan 2011년 9월 22일
Or B=A(aidx, :);

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Vincent
Vincent 2011년 9월 22일
Works like a charm, thanks! This very useful option for ismember is way to hidden :-/
I'm working since two months now with Matlab and used a dirty workaround until now...

Jan
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.

Vincent
Vincent 2011년 9월 22일
Nice, this leveled up my self-esteem :)
but the code takes much more time to be written and doesn't stay readable. And of course, I gave just a tiny example above; my datasets are usually betwenn 600x40 and 2000x40 entries big
But thank you anyway for this hint
  댓글 수: 1
Jan
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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by