# Dependent variable formatting in find_best_table_nd

1 view (last 30 days)
Alex on 24 Aug 2017
Answered: Tucker McClure on 25 Aug 2017
I'm trying to get my mind around find_best_table_nd.m and make sense of the formatting required for dependent variable.
In my application both my independent variable and dependent variable has three values (call them r_in, g_in, and b_in, and r_out, g_out, and b_out). I'm applying the function outside of Matlab and the r_out, g_out, b_out are returned to me as an nx3 array. I'm trying to figure out how I need to reformat that array to use the table optimization function. I'm assuming the function should look sometime like this, but maybe I'm off base ...
[x_f, z_f, mse] = find_best_table_nd({r_in, g_in, b_in}, {r_out, g_out, b_out}, [17 17 17]);
Any hints on how to get from an nx3 to the proper format for find_best_table_nd would be greatly appreciated.

Tucker McClure on 25 Aug 2017
Hi Alex,
Good question. First, I'll say that the help text for that function clearly has some copy-and-paste errors in it -- my mistake. The example code for Table Breakpoint Optimization shows the proper use.
However, your problem has an extra difficulty. While all of the functions in Table Breakpoint Optimization are meant to optimize a table, it sounds like you need to optimize three tables (each of r_out, g_out, and b_out). Would it be acceptable to optimize the size of each of these separately, with three separate calls to find_best_table_nd()? That would mean that you'd have (for your example) 3 independent variables each with 17 data points for each individual output table (so, 9 tables of independent variables and 3 tables of dependent variables, each 17x17x17, in all).
rgb_in = {r_in, g_in, b_in};
dims = [17 17 17];
[rgb_fit_r, r_f] = find_best_table_nd(rgb_in, r_out, dims);
[rgb_fit_g, g_f] = find_best_table_nd(rgb_in, g_out, dims);
[rgb_fit_b, b_f] = find_best_table_nd(rgb_in, b_out, dims);
If you need for the tables of dependent values to be the same for each of r_out, g_out, and b_out, then there's a missing piece of information: what would be optimal? Would optimally mean minimizing the sum of squared differences across all tables taken together? It's not by default clear that the value of errors on table 1 would be consistent with the values of errors on table 2, etc. For instance, those tables might have different units. (Though, for your example, you appear to be working in color space, and the errors may well be equally valued.) Well, regardless, if you need to have the same 3 tables of independent variables for all three output tables, then I have to say that this functionality doesn't exist. The code could be modified for this purpose. If z_0 were a cell array, one could write a loop around the point at which it's used, summing up the errors against all tables (each element of z_0). It would be easier if use_interpolator were set to false in the case that z_0 were a cell array. I could potentially add this to the code base, but the fact that each table would have equal weight means it's not actually very general. I could add a weights input that would be required to have the same size as z_0, but this is getting to be an interface change, and I'm not yet even sure this would solve your problem. Here's what I think would work for what I take to be your basic case.
if ~iscell(z_0), z_0 = {z_0}; end; % Make z_0 a cell array to support the "normal" syntax.
se = zeros(size(z_0{1}));
for index = 1:length(z_0)
z_i = interpn(x_0_meshs{:}, z_0{index}, x_i_meshs{:}, method);
se = se + (z_0{index} - interpn(x_i_meshs{:}, z_i, ...
x_0_meshs{:}, method)).^2;
end
mse = sum(se(:)) / (length(z_0) * numel(z_0{1}));