function [unique_positions, value_sums] = unique_totals_old(positions, values) % function [unique_positions, value_sums] = unique_totals_old(positions, values) % % Function to return sums of values found at each unique position. % % Inputs: % positions NxD - may not provide DxN unless D==1 % values Nx1 - can provide 1xN % % Outputs: % unique_positions MxD - M depends on input % value_sums Mx1 % Iain Murray, November 2006. Minor reformating November 2007. % This file is for historical interest and testing. A newer version is % available. % Sanitize input if isempty(positions) && isempty(values) unique_positions = []; value_sums = []; return end if min(size(values)) ~= 1 error('Values must be a vector.'); end values = values(:); [N, D] = size(positions); if (N == 1) && (D == length(values)) positions = positions'; [N, D] = size(positions); end if length(values) ~= N error('imurray:unique_totals:invalid_input', ... ['You must have the same number of positions and values.\n',... 'Is the NxD parity correct on the positions matrix?']); end % Sorting by position makes the values that need to be summed adjacent if D>1 [unique_positions, dummy, position_indexes] = unique(positions, 'rows'); [sorted_positions, idx] = sort(position_indexes); else [sorted_positions, idx] = sort(positions); end sorted_values = values(idx); % Find the blocks and do the sums position_block_ends = [find(diff(sorted_positions)); N]; if D == 1 unique_positions = sorted_positions(position_block_ends); end cum_values = cumsum(sorted_values); value_sums = diff([0; cum_values(position_block_ends)]);