Oliver Amundsen
Oliver Amundsen

Reputation: 1511

Insert missing numbers in ordered vector with repeated elements

I have the following case:

A = [1 2 3 4 5 6 7 8 9 10];
B = [1 1 1 6 8 8 10];

How can I create a vector C that joins A and B, inserting the missing numbers in the series but keeping the repeated numbers in A? C will look like this:

C = [1 1 1 2 3 4 5 6 7 8 8 9 10];

Upvotes: 2

Views: 81

Answers (1)

learnvst
learnvst

Reputation: 16195

Find the logical index of A in B, remove them with ~, concatenate the resulting vectors and sort . . .

C=sort([A(~ismember(A,B)) B])

Upvotes: 3

Related Questions