How do you check if all the values in a vector are in another vector as many or more times?
19 views (last 30 days)
Show older comments
Hello everyone, I have a question that is probably really easy but that is driving me crazy.
Let's say I have two vectors, a small one and a long one. I would like to check if all the values that are in the small vector are also in the long vector as many or more times than in the small vector.
For example if I have a vector a=[1 1 3 4 4] and a vector b=[3 3 4 7], I would like matlab to tell me that in order to contain all the values of b, a would need a 7 and another 3.
I hope this is clear, I know of the ismember function but I can't figure how to make it work here.
Thanks infinitely for the help!
0 Comments
Accepted Answer
Guillaume
on 15 Dec 2017
fullset = [1 1 3 4 4];
baseset = [3 3 4 7];
[basesetvalues, ~, subs] = unique(baseset);
basesetcount = accumarray(subs, 1); %histogram of baseset values
[found, where] = ismember(fullset, basesetvalues);
fullsetcount = accumarray(where(found)', 1, [numel(basesetvalues), 1]);
diffcount = basesetcount - fullsetcount;
notenough = diffcount > 0;
out = table(basesetvalues(notenough).', diffcount(notenough), 'VariableNames', {'Value', 'MissingCount'})
3 Comments
Guillaume
on 18 Dec 2017
I actually found another way to solve my problem
If by another way you mean KL's answer, then as per Stephen's comment it doesn't work.
It´s actually a little bit complicated
Well, what you want is not that common so there's nothing built-in to do it. It's not actually that complicated, all it does is compute the histogram of the required numbers in both sets and compare them.
More Answers (2)
KL
on 15 Dec 2017
I'd recommend using intersect since you want to handle repeating elements as well. For example,
a=[1 1 3 4 4];
b=[3 3 4 7];
[~,~,ind] = intersect(a,b);
res = b(~(ismember(1:numel(b),ind)))
and the result is
res =
3 7
3 Comments
Stephen23
on 18 Dec 2017
Edited: Stephen23
on 18 Dec 2017
Because intersect returns only the first index if a value is repeated then the position/s of the duplicate elements will never be matched by ismember, which means that this answer is flawed and will never work. This is easy to check:
>> a = [1,1,3,3,4,4]; % note extra 3!
>> b = [3,3,4,7];
>> [~,~,ind] = intersect(a,b);
>> b(~(ismember(1:numel(b),ind)))
ans =
3 7
Whereas according to the original question, the answer should be just 7.
See Guillaume's answer for code that actually does what the question requests.
Jos (10584)
on 18 Dec 2017
Edited: Jos (10584)
on 18 Dec 2017
a = [1 1 3 4 4]
b = [4 7 3 3]
[bu,~,j] = unique(b) ;
n_present = countmember(bu,a) ;
n_needed = accumarray(j,1).' ;
n_missing = max(n_needed - n_present,0) ;
% bu(k) should be added n_missing(k) times to a
disp([bu ; n_missing].')
COUNTMEMBER is a small " pick-of-the-week" utility I wrote, that can be downloaded here: https://uk.mathworks.com/matlabcentral/fileexchange/7738-countmember-a-b-
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!