Can we do something like this similar to union()?

1 view (last 30 days)
Currently union() returns non-repeated result. But I want repeat result like the following,
>> myunion([2 3 3 5], [1 3 5])
ans =
1 2 3 3 5
I can write for-loop and if-statement to do this. But is there a better or easy way?
Thanks Kevin
  4 Comments
John D'Errico
John D'Errico on 11 Feb 2018
Edited: John D'Errico on 11 Feb 2018
I'm a bit confused by the logic in what you want. As you wrote it, you want
myunion([2 3 3 5], [1 3 5])
to return the vector [1 2 3 3 5]. But by extension, if you simply swapped the arguments, calling it as
myunion([1 3 5], [2 3 3 5])
then it would return [1 2 3 5].
SHIVER. This is just asking to create the code from hell, i.e., code that will create all sorts of strange bugs, arising from that non-symmetrical behavior. Any code that claims to be a union should also be symmetrical in the arguments.
My personal prediction is that this code will soon be the source of a new, frenzied post, "Why do I have this strange bug in my code? Forewarned is forearmed. :)

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 11 Feb 2018
Edited: Stephen23 on 13 Feb 2018
Method one: unique, arrayfun, ones:
>> A = [2,3,3,5];
>> B = [1,3,5];
>> fun = @(n)n*ones(1,max(nnz(A==n),nnz(B==n)));
>> C = arrayfun(fun,unique([A(:);B(:)]),'uni',0);
>> U = [C{:}]
U =
1 2 3 3 5
Note: the arrayfun call is just for convenience, not for speed!
Method two: unique, histc, repelem:
>> A = [2,3,3,5];
>> B = [1,3,5];
>> Q = unique([A(:);B(:)].');
>> repelem(Q,max([histc(A,Q);histc(B,Q)]))
ans =
1 2 3 3 5
  3 Comments
Stephen23
Stephen23 on 13 Feb 2018
Edited: Stephen23 on 13 Feb 2018
@Kevin: thank you for the feedback. I will include your suggested fix into my answer (for future readers).

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 10 Feb 2018
Edited: John D'Errico on 10 Feb 2018
My guess is that your ambiguous example was not even representative of your problem, given your words. So let me guess and try to answer your words. :)
Your words said that you want the union, but one that allows repeats. In that case it should have produced:
myunion([2 3 3 5], [1 3 5])
ans =
1 2 3 3 3 5 5
since there were 3 copies of the number 3, and 2 copies of the number 5.
If so, then the answer is trivial.
myunion = @(X,Y) sort([X,Y]);
You might need to worry about the orientation of the vectors, in case they might be row OR column vectors or both. But that is an easy change.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!