spacer
{Arrest This Man, He Talks In Maths } spacer

Blog : Archives : Homepage

With your feet in the air, and your head on the ground . . .

spacer
spacer

{Tuesday, July 15, 2003}

 
Fuck yeah.

Bee-atch.



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.
%

start_time = clock;

%example inputs:

%set_sizes = [5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26];
% slop = .010;
%target_ratios = [1.5 1.6 1.7 1.8 1.9 2.0];

max_out = min(set_sizes)-1;

[junk num_set_sizes] = size(set_sizes);
[junk num_target_ratios] = size(target_ratios);
[junk num_slop] = size(slop);

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:

for i=1:20
fprintf('%s','X');
end
fprintf('\n');

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[junk num_dims] = size(num_combos);

low_threshold = zeros(tot_num_bas,1);
high_threshold = zeros(tot_num_bas,1);
overlap_pairs = zeros(tot_num_bas,1);
inside_pairs = zeros(tot_num_bas,1);

% main loop for producing "big and small" matrices

for i=1:tot_num_bas

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% More progress bar code:

progress=round(100*(i/tot_num_bas));

if mod(i,round(tot_num_bas/20)) == 0
fprintf('%s','X');
end

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cur_bas = zeros(num_set_sizes,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code to extract N-dimensional cartesian coords from single index

single_index=i;

for j=1:num_dims
it_index = num_combos(num_dims-j+1);
cart_coords(j) = 1+mod(single_index-1,it_index);
single_index = ceil(single_index / it_index);
end

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

these_real_pairs = zeros(2*num_target_ratios,2);

%%%%%%%%%%%%%%%%%%%
% The meat:
%
% First, construct a "big and small" matrix:

for j=1:num_target_ratios
this_pair = nck_matrix{j}(cart_coords(num_target_ratios-j+1),:);
for k=1:2
l_index = matches_w_ratios{j}(this_pair(k),1)-max_out;
s_index = matches_w_ratios{j}(this_pair(k),2)-max_out;
cur_bas(s_index,1) = cur_bas(s_index,1)+1;
cur_bas(l_index,2) = cur_bas(l_index,2)+1;
these_real_pairs(((j-1)*2+k),:) = [l_index s_index];
end
end

% Second, calculate thresholds

low_threshold(i) = max(find(cur_bas(:,1)));
high_threshold(i) = min(find(cur_bas(:,2)));

% 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

bas(:,:,i) = cur_bas;
end

inside_stats = [low_threshold high_threshold overlap_pairs inside_pairs];

% find best set, based on number of inside pairs:

[max_inside_val, max_inside_index] = max(inside_pairs)
[max_overlap_val, max_overlap_index] = max(overlap_pairs)

max_cart_coords = single_to_cartesian(max_inside_index,num_combos)

real_goddamn_pairs = zeros(num_dims*2,3);

% 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

Comments: Post a Comment
spacer