matlab - matching vectors based on string -
i'm trying used doing things in vectors rather looping. question have have 2 vectors. vector 2 64x2 cell first column contains strings second contains numbers. want match vector 1 & 2 on string value , copy numeric value on new field in vector 1 if possible?
vector 1 vector 2 ghj abc 352 lmn ghj 62 opq lmn 3698 opq 12
i vector 1 below,
vector 1 ghj 62 lmn 3698 opq 12
actually guess problem solved deleting row abc vector 2.
you can use ismember
compare 2 arrays. have strings , numbers in 1 array you'll have use cell array.
a = {'abc', 352; 'ghj', 62; 'lmn', 3698; 'opq', 12} b = {'ghj'; 'lmn'; 'opq'}; matchindex = ismember(a(:,1), b) %put vector 2 first matchindex = 0 1 1 1
this return logial vector length of containing 1 (true) data in found in b. elsewhere, returns 0 (false). check documentation of ismember
more examples!
can use logical array logical indexing a
c = a(matchindex,:); c = 'ghj' [ 62] 'lmn' [3698] 'opq' [ 12]
Comments
Post a Comment