for dim = [1, 3] N = 101; repeats = 100; num_records = N*repeats; pos = randn(N, dim); values = randn(N, repeats); inputs = repmat(pos, repeats, 1); outputs = reshape(values, num_records, 1); perm = randperm(num_records); inputs = inputs(perm, :); outputs = outputs(perm); tic; [unique_positions_old, value_sums_old] = unique_totals_old(inputs, outputs); toc tic [unique_positions, value_sums] = unique_totals(inputs, outputs); toc % Check against old implementation: testequal(unique_positions, unique_positions_old); testclose(value_sums_old, value_sums); % Check against ground truth (note that I don't mind what order the postions are % declared in) tots = sum(values, 2); [tot_vals, idx] = sort(tots); [tot_vals2, idx2] = sort(value_sums); testclose(tot_vals2, tot_vals); testequal(pos(idx,:), unique_positions(idx2,:)); end test_exit