Amin Nourmohammadi
Amin Nourmohammadi

Reputation: 13

Is there a straightforward way to concatenate two or more matrices and at the same time avoid repeating elements?

Say we want to concatenate two matrices a=[2 3 6] , b=[0 9 3 2 8 2] but we don't want any repetitive elements in the concatenated matrix. in other words we want c to be c=[2 3 6 0 9 8] Is there a built-in function that does that for us?

Upvotes: 1

Views: 40

Answers (1)

OsJoe
OsJoe

Reputation: 259

% You could use the union function to accomplish this

a = [2 3 6];        % array a
b = [0 9 3 2 8 2];  % array b

% Use the union function to concatenate a and b. It lists an item that appears in either array once.

c = union(a,b, 'stable');  %  adding 'stable' keeps your current order

Upvotes: 4

Related Questions