function [inside_stats, bas] = sm_ratio_finder(target_ratios,set_sizes,slop)
%SM_RATIO_FINDER
%
%usage: [inside_stats, bas] = sm_ratio_finder(target_ratios,set_sizes,slop)
%
%creates all possible pairings for an input set of integers (set_sizes), picks those
%matching (within the range 'slop') an input set of ratios (target_ratios), and then
%looks for the '2-from-each-ratio' combination of pairings that includes the most
%'within threshold range' pairs.
%
%'slop' may be a vector (with the same length as target_ratios) or it may be a scalar.
%
if num_slop == 1
slop = slop*ones(1,num_target_ratios);
else
if num_target_ratios ~= num_target_ratios
error('slop and target_ratios dimensions do not match');
end
end
num_ratios = zeros(num_set_sizes,num_set_sizes);
% compute ratios for all pairings:
for i=1:num_set_sizes
for j=1:num_set_sizes
num_ratios(i,j) = set_sizes(i)/set_sizes(j);
end
end
% find matches to target ratio set:
for i=1:num_target_ratios
[a b] = find(num_ratios>(target_ratios(i)-slop(i)));
[c d] = find(num_ratios<(target_ratios(i)+slop(i)));
matches{i} = intersect([a b],[c d],'rows');
[num_matches,junk] = size(matches{i});
if (num_matches < 2)
fprintf('%s %2.2f \n','failure to find two pairs within range for ratio: ',target_ratios(i));
error('exiting - no successful pairing');
end
end
% reformat, to create a cell array of one number pair / exact ratio matrix per target ratio
for i=1:num_target_ratios
[num_matches junk] = size(matches{i});
matches_w_ratios{i} = zeros(num_matches,3);
% matches_w_ratios{i}(:,1:2) = matches{i};
for j=1:num_matches
matches_w_ratios{i}(j,1) = set_sizes(matches{i}(j,1));
matches_w_ratios{i}(j,2) = set_sizes(matches{i}(j,2));
matches_w_ratios{i}(j,3) = num_ratios(matches{i}(j,1),matches{i}(j,2));
end
end
% prepare to calculate "big and small" matrix, calculate thresholds, & count "inside" pairings:
big_and_small = zeros(num_set_sizes,2);
% for each target ratio, create list of all different "2 pair" sets
for i=1:num_target_ratios
[num_matches, junk] = size(matches_w_ratios{i});
index_vector = [1:num_matches];
nck_matrix{i} = nchoosek(index_vector,2);
end
% calculate total number of "N*2 pair" full experimental sets
tot_num_bas = 1;
for i=1:num_target_ratios
[num_combos(i) junk] = size(nck_matrix{i});
tot_num_bas = tot_num_bas*num_combos(i);
end
num_combos
bas = zeros(num_set_sizes,2,tot_num_bas);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is just a progress bar:
% Third, count "overlap" and "inside" pairs
% (pairs with one or two members, respectively, inside threshold limits)
overlap_pairs(i) = sum(sum(cur_bas(high_threshold(i):low_threshold(i),:)));
for j=1:(2*num_target_ratios)
if and((these_real_pairs(j,2)>=high_threshold(i)),(these_real_pairs(j,1)<=low_threshold(i)))
inside_pairs(i) = inside_pairs(i)+1;
end
end
% go back and find the actual numbers & ratios for chosen optimal set
for i=1:num_dims
set_pair = nck_matrix{num_dims-i+1}(max_cart_coords(i),:);
for j=1:2
real_goddamn_pairs((i-1)*2+j,:) = matches_w_ratios{num_dims-i+1}(set_pair(j),:);
end
end
real_goddamn_pairs
etime(clock,start_time)
(took me freakin' long enough) posted by Miles 6:59 PM