How do you check if all the values in a vector are in another vector as many or more times?

조회 수: 22 (최근 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!

채택된 답변

Guillaume
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
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.
Florent Dueme
Florent Dueme 2017년 12월 18일
편집: Florent Dueme 2017년 12월 18일
Yep that´s what I meant I though there would be some built-in function to do it. I was in a bit of a hurry so I actually found a way to go around the problem instead of using these solutions, I was hoping to take a look at them later (I don´t like to just copy paste code I don´t understand I feel like i´m not learning anything that way and I´m setting myself up for some trouble). I accepted KL´s answer because it looked like it could work even though I didn´t test it myself, sorry if I mislead anyone!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KL
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
Stephen23 2017년 12월 18일
편집: Stephen23 2017년 12월 18일
This answer does not work correctly because it uses intersect.
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)
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-

카테고리

Help CenterFile Exchange에서 图形对象的标识에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!