How can I find all possible pairs within a range that result in the same average?
3 次查看(过去 30 天)
显示 更早的评论
I'm trying to figure out how to output multiple pairs of values that happen to have the same average (added together and divided by 2) within a range. For example, if I set an array to be:
array= [0:1:100]
and I want all possible pairs that output an average of 2.
avg= 2
So for example, 2 pairs would be (0,2) and (2,2) and the avg. of both of these seprately is 2.
I'm sure I'll probably need a for loop, but I've read here on several function such as permutations or combinations, but I feel like this should be more simple. Obviously I'll have a lot more conditions involved, but the basic premise of getting this to work is somehow eluding me, so I'd appreciate a little nudge in the right direction here on how to go about this. Thank you!
采纳的回答
Torsten
2022-6-9
编辑:Torsten
2022-6-9
Do you know if there's any way that this can be done by just using for loops and nested for loops with if statements to get the same result?
Sure. A very slow version would be
mean_compare = 2.0;
a = 0:100;
n = numel(a);
ifound = 0;
b = [];
tic
for i = 1:n
value1 = a(i);
for j = 1:n
value2 = a(j);
if value1 + value2 == 2*mean_compare
ifound = ifound + 1;
b = [b,[value1;value2]];
end
end
end
toc
b
2 个评论
Torsten
2022-6-10
编辑:Torsten
2022-6-10
A better solution is the following:
Let
m = min(a)
and
M = max(a)
Let
feasible_range = min(mean_compare - m, M - mean_compare)
Consider the points
union( (m,m) , (m-(1:feasible_range).',m+(1:feasible_range).') , (m+(1:feasible_range).',m-(1:feasible_range).') )
The intersection with
a x a
is the solution (at least if "a" is an array of integers).
更多回答(3 个)
KSSV
2022-6-9
a = 0:10 ;
b = permn(a,2) ;
idx = mean(b,2)==2 ;
iwant = b(idx,:)
You can download the function permn from the file exchange: https://in.mathworks.com/matlabcentral/fileexchange/7147-permn
2 个评论
Steven Lord
2022-6-9
As long as your arrays aren't that large just brute force it:
array= 0:1:100;
desiredAverage = 2;
ind = find(array+array.' == 2*desiredAverage)
[r, c] = ind2sub(numel(array)*[1 1], ind)
B = array([r, c]) % 5 pairs of numbers each of whose average is 2
mean(B, 2) == desiredAverage % all true
If you had to do this with a loop (because that's one of the requirements of your homework assignment, for instance) you only need one.
Hint: I'm thinking of two numbers whose average is 10. One of the numbers is 5. What's the other? How did you determine the answer?
What if instead I told you one of the numbers was 17. What's the other?
0 个评论
DGM
2022-6-10
I know Steven hinted at it, but I'm just going to give an example. Since the assignment forces you to accept bad decisions (and you've already accepted them), I might as well demonstrate that the consequences aren't trivial.
candidatevalues = 0:1:100; % the set
desiredAverage = 2; % the target
companionvalues = 2*desiredAverage - candidatevalues;
isvalidpair = ismember(companionvalues,candidatevalues);
validpairs = [candidatevalues(isvalidpair); companionvalues(isvalidpair)].' % these are the pairs
mean(validpairs,2) % their average is as expected
Despite the use of ismember(), this naive algebra approach can be much faster than any of the examples given to meet the loop-based requirements. For a set size of 10E3 and a target mean of 25, on my hardware in R2019b, the 3-line example above is between 1400 to 35000 times as fast as the other examples. For a set size of 100, the smallest speed advantage is a factor of 5. Is my example ideal? I doubt it, but it's a remarkable amount better than what's been deemed acceptable.
I'm no compsci professor, but I'm sure there's a lesson to be found here regarding computational complexity.
3 个评论
DGM
2022-6-10
Ah. It's good to explore. I'm just too eager to critique what I think are unhelpful homework tropes, that's all.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!