age matching between two groups

6 次查看(过去 30 天)
Hi,
I have two vectors containing the age (integers) of subjects. At the beginning, there is a big difference between the average of two groups. My goal is to swap the elements of two vectors in a way that makes the two averages as close as possible together. Any idea how I can do this?
Thank you!

采纳的回答

Roger Stafford
Roger Stafford 2016-3-17
There is a very crude brute force method, but it is only practical for a limited range of sizes of the two groups. I suppose first that you want to keep the respective sizes of the two groups unchanged. Let n1 be the number in one group and n2 that in the other group, and let s be the sum of all the ages in the two groups. Use Matlab's 'nchoosek' function to obtain all possible n1 subsets of the n1+n2 total. For each subset find the sum of its elements, s1, and compute abs(s1/n1-(s-s1)/n2) and then choose the minimum of all these absolute differences.
In case you allow the sizes to vary, do the above for all possible values of n1 in 0 < n1 < total_size, and choose the minimum of all these minima.
As I have indicated, I do not recommend the above for very large values of n1+n2.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by