Matlab: Update elements in array by identifier -


i have large array ~500.000 rows of form

[ id1  id2  value1  value2  0  0 ] 

and another, smaller array b (~20.000 rows) rows of identifiers a

[ id1  id2  value3  value4 ] 

all pairs of ids in b exist in a. want update values of b @ positions respectively values of id1 , id2 match. (row-)order of new array may arbitraty.

an example:

a = 1 1  3 5 0 0     1 2  6 4 0 0     1 3  3 1 0 0     2 1  3 8 0 0     3 4  0 2 0 0  b = 2 1  7 4     1 1  2 1 

should yield

c = 1 1  3 5 2 1     1 2  6 4 0 0     1 3  3 1 0 0     2 1  3 8 7 4     3 4  0 2 0 0 

iterating through each element in b loops takes incredibly long. hope there faster way.

you can use ismember obtain indices of rows "id1" , "id2" match, , update last 2 columns corresponding values b:

c = a; [tf, loc] = ismember(b(:, 1:2), a(:, 1:2), 'rows'); c(loc, 5:6) = b(:, 3:4); 

Comments