How do you check if all the values in a vector are in another vector as many or more times?
37 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Guillaume
2017-12-15
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 个评论
Guillaume
2017-12-18
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.
更多回答(2 个)
KL
2017-12-15
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 个评论
Stephen23
2017-12-18
编辑:Stephen23
2017-12-18
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)
2017-12-18
编辑:Jos (10584)
2017-12-18
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!