3

I have a large array A with ~500.000 rows of the form

[ id1  id2  value1  value2  zero  zero ]

and another, smaller Array B (~20.000 rows) with rows with some of the identifiers from A

[ id1  id2  value3  value4 ]

All the pairs of IDs in B exist in A. I want to update the values of B into A at the positions where respectively the values of id1 and id2 match. The (row-)order of the new array may be 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 A for each element in B with for loops takes incredibly long. I hope there is a faster way.

1 Answer 1

3

You can use ismember to obtain the indices of the rows where "id1" and "id2" match, and then update the last two columns with the corresponding values from B:

C = A;
[tf, loc] = ismember(B(:, 1:2), A(:, 1:2), 'rows');
C(loc, 5:6) = B(:, 3:4);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.