Comparing strings in cell arrays of different sizes *Most efficient*
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I have got two cell arrays, actually I got 3. example (excuse any typos im writing this off the top of my head, i just want to know the logics behind it)
Cellarray_1 = { 'a'; 'access'; 'also'; 'always'; 'and'; 'arrived' };
Cellarray_2 = [ 1 23 0 10 13 34 2 4 5 1 1];
Cellarray_3 = { 'a'; 'about'; 'above' ;'access'; 'addition'; 'also'; 'alter'; 'always'; 'akin' ; 'and'; 'arrived'};
The aim is to check which strings in Cellarray_1 are in Cellarray_3. From this find the index values to get the relevant element in Cellarray_2.
so the output should be a vector:
output = [1 10 34 4 1 1]
This is achieved because ("a", "access", "also", "always", "and", "arrived") appear in Cellarray_3 and there corresponding index values point to Cellarray_2 index values getting the data
I have got this so far:
for i=1:numel(Cellarray_1)
indexfind = strcmp(Cellarray_3,Cellarray_1(i));
testoutput(i) = Cellarray_2 (find(indexfind == 1))
end
Is there a more efficient way. As far as I can tell (in my tired state) there is but, I was hoping you Pros can guide me in the right direction.
Thanks in advance.
댓글 수: 0
채택된 답변
Teja Muppirala
2012년 12월 10일
How about using the ISMEMBER function?
Cellarray_2(ismember(Cellarray_3,Cellarray_1))
ans =
1 10 34 4 1 1
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!