Create a function that receives two vectors and returns a vector that are the elements of a vector that are not in the other vector

28 次查看(过去 30 天)
I am trying to create the following function:
Program a function whose call has two possible versions: 1. [u] = exclusion_select (u_origin, v) or 2. [u, u_excl] = exclusion_select (u_origin, v) where the input parameters u_origin and v are two numeric vectors (u_origin is in ascending order) and the program must extract from u_origin the elements of v that are in u_origin and produce as output parameters the vector u (identical to u_origin but without the elements excluded) and in the second option also the vector u_excl that contains the elements that have been excluded from u. Example: With u_origin = [1 7 23 45 62] and v = [45 35 1 15] the call [u, u_excl] = exclusion_select (u_origen, v) u = [7 23 62] u_excl = [45 1]. Tip: Use binary search as an auxiliary function that is called within the exclusion_select function
And I've seen that this can be done very easily by using the setdiff command
function [u,u_excl]=exclusion_select(u_origen,v)
[u,I]=setdiff(u_origen,v);
N=1:length(u_origen);
K=setdiff(N,I);
for i=1:length(K)
u_excl(i)=u_origen(K(i));
end
end
But I want to see how it would be a program that does not use this matlab command, as would be the case if it did not use setdiff? Thank you.
  3 个评论
Nathali Londoño Torres
I tried to do this but it does not work for me:
function [u,u_excl]=exclusion_select3(u_origen,v)
for i=1:length(v)
I(i)=find(u_origen==v(i));
u_excl(i)=u_origen(I(i));
end
for j=1:length(v)
M(j)=find(~(u_origen==v(i)));
u(j)=u_origen(M(j));
end
end
What can I do to get the program working? Thank you very much.
Jan
Jan 2019-3-28
编辑:Jan 2019-3-28
"It doesn' t work" is a weak description of your problem. If you want others to help you, please mention, what the problem is.
What about this: Define u and u_excl as emopty matrices. Run a loop over all elements of u_origen. Check for each element, if it exists in v: if any(u_origen(k) == v). If so, append the element to u, otherwise to u_excl.
Instead of if any(u_origen(k) == v) you can use the mentioned binary search. Take into account that "u_origin is in ascending order", so you have to run a loop over the elements of v instead. This might clarify, why the 2nd output is [45, 1] (the order in which the elements occur in v) and not [1, 45].
By the way, a simplification of your code:
function [u, u_excl] = exclusion_select(u_origen, v)
[u, I] = setdiff(u_origen,v);
N = 1:length(u_origen);
u_excl = u_origen(setdiff(N, I));
end

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2019-3-28
编辑:Jan 2019-3-28
A brute approach, which creates a huge temporary matrix for large inputs:
u_origin = [1 7 23 45 62];
v = [45 35 1 15]; % u = [7 23 62] u_excl = [45 1].
function [u, u_excl] = exclusion_select(u_origen, v)
m = any(u_origin == v.', 1);
u = u(m);
u_excl = u(~m);
end
This replies u_excl as [1, 45], not as the strange [45, 1] as wanted in the question. It is hard to guess, what the general rule is to create the latter, while [1, 45] is the same order as the input u.
But more important might be tis detail: Tip: Use binary search as an auxiliary function
As long as you do not show us, how your "binary search method" is called, we cannot show you, how to use it. Try it by your own at first.
My code is sufficient for tiny examples only, not for productive work and it wil not match your homework question also.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by