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

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-2-10
Kevin's "Answer" moved here:
Sorry, my fault. I should be more specific in my first post.
>> myunion([2 3 3 5], [1 3 5])
ans =
1 2 3 3 5
  • The first element (in the output vector) is 1 because the second input vector has a 1.
  • The second element (in the output vector) is 2 because the first input vector has a 2.
  • The third and forth elements (in the output vector) are 3 because the first input vector has [3 3]. Now if first input vector has [3 3 3], then output vector vector should have three 3's, i.e. I want the output vector should have the repeated elements (number of repeated elements = maximum of the number of elements in either input vector).
So MATLAB's union() is almost perfect except that it does not returned the repeated elements.
>> union([2 3 3 5], [1 3 5])
ans =
1 2 3 5
union() would be perfect if the output vector has two 3's since the first input vector has 2 3's.
Hope this is clear.
Kevin
John D'Errico
John D'Errico 2018-2-11
编辑:John D'Errico 2018-2-11
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. :)

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-2-11
编辑:Stephen23 2018-2-13
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 个评论
Kevin
Kevin 2018-2-12
Hi Stephen,
You are brilliant. Thank you for your idea on using HIST. Your method 2 is very elegant and much faster.
Found a corner case that breaks method 2,
A = [3 3]; B = [3 3 3];
This causes problem into the call of HIST because Q is a scalar and treated as number of bins. So use HISTC instead.
Fix:
Q = unique([A(:); B(:)].');
U = repelem(Q, max([histc(A, Q); histc(B, Q)]));
Thank you very much for your idea. Kevin
Stephen23
Stephen23 2018-2-13
编辑:Stephen23 2018-2-13
@Kevin: thank you for the feedback. I will include your suggested fix into my answer (for future readers).

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
John D'Errico 2018-2-10
编辑:John D'Errico 2018-2-10
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.

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by