Optimisation of array to smaller size
Show older comments
I have an array of size 30x20 with non linear breakpoints (Rows & Columns), that I want to convert to a smaller size of 20x15 where the new breakpoints for this smaller array are unknown and require calculating.
I have started by trying to create a linearly spaced array for the new breakpoints, which as an initial guess is ok, however my purpose here is to minimise the error between the larger and smaller arrays. Given that the original arrays breakpoints are non-linear, the assumption that the new array can be linearly spaced, is not ideal and does not minimise the error - My current attempt looks like this;
original_map = cell2mat(app.table_handle.Data);
original_xbp = str2double(string(app.table_handle.ColumnName));
original_ybp = str2double(string(app.table_handle.RowName));
rows = size(app.table_handle.Data, 1);
cols = size(app.table_handle.Data, 2);
new_rows = size(app.table2_handle.Data, 1);
new_cols = size(app.table2_handle.Data, 2);
[col_indices, row_indices] = meshgrid(1:cols, 1:rows);
[new_col_indices, new_row_indices] = meshgrid(linspace(1, cols, new_cols), linspace(1, rows, new_rows));
reduced_map = interp2(col_indices, row_indices, original_map, new_col_indices, new_row_indices, 'cubic');
Would it be possible to optimise the breakpoint values to minimise the error between the original array and the new array, then interpolate the original array values to those new breakpoint values? I tried to use fminsearch but ran into problems due to the array sizes not being the same between X & Y breakpoint arrays.
For reference, this is my attempt with using fminsearch;
function error = new_function(new_xbp, new_ybp, original_map, original_xbp, original_ybp)
[X, Y] = meshgrid(new_xbp, new_ybp);
new_map = interp2(original_xbp, original_ybp, original_map, X, Y);
[dxx, dxy, dyy] = gradient2(new_map);
error = sum(sum(dxx.^2 + dxy.^2 + dyy.^2));
end
original_map = cell2mat(app.table_handle.Data);
original_xbp = str2double(string(app.table_handle.ColumnName));
original_ybp = str2double(string(app.table_handle.RowName));
new_rows = size(app.table2_handle.Data, 1);
new_cols = size(app.table2_handle.Data, 2);
initial_xbp = linspace(original_xbp(1), original_xbp(end), new_cols);
initial_ybp = linspace(original_ybp(1), original_ybp(end), new_rows);
options = optimset('Display', 'iter');
[optimal_xbp, ~, ~, output] = fminsearch(@(x) new_function(x, initial_ybp, original_map, original_xbp, original_ybp), initial_xbp, options);
[optimal_ybp, fval] = fminsearch(@(y) new_function(optimal_xbp, y, original_map, original_xbp, original_ybp), initial_ybp, options);
[X, Y] = meshgrid(optimal_xbp, optimal_ybp);
reduced_map = interp2(original_xbp, original_ybp, original_map, X, Y);
Any help is really appreciatted!
Accepted Answer
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!